ServiceStack AutoQuery join use

378 Views Asked by At

After reading the documentation, I am not sure but I have come to the conclusion that when creating QueryDb, you cannot choose the columns to join by? And I am under the impression, you must have DTO object to copy to? You cannot copy to a regular object or a dynamic object?

public class SampleAutoQueryDb : QueryDb<MailResponseDetailOrm, object>, ILeftJoin<MailResponseDetailOrm, MailResponseOrm> { }

Can anyone provide any insight on joining my MailResponseOrm to MailResponseDetailOrm. MailResponseDetailOrm has 5 fields namely the Email address. And I would like MailResponseOrm to be joined to it by Email as well. I also, for good measure do not want to alter either columnname. Would I have to create a custom implementation or a service to do this?

UPDATE

Here is my code as posted below:

        [Alias("MailReportsDetail")]
        public class MailResponseDetailOrm
        {
            public string Email { get; set; }

            public int ID { get; set; }

            [Alias("RespDate")]
            public DateTime? AddedDateTime { get; set; }

            [Alias("DLReport")]
            public string Action { get; set; }

            public string ActionDetail { get; set; }

            public string IP { get; set; }

            public string UserAgent { get; set; }

            public string EmailReferrer { get; set; }
        }

    [Alias("MailReports")]
    public class MailResponseOrm
    {
        public int ID { get; set; }

        public string Email { get; set; }

        public string Address1 { get; set; }

        public string Address2 { get; set; }

        public string City { get; set; }

        public string Company { get; set; }

        public string Contact { get; set; }

        public string Country { get; set; }

        [Alias("LastMail")]
        public DateTime? ModifiedDateTime { get; set; }

        [Alias("LastReport")]
        public string Action { get; set; }

        public DateTime? OptOut { get; set; }

        public string Part { get; set; }

        public string Phone { get; set; }

        public string PostalCode { get; set; }

        public string Source { get; set; }

        public string State { get; set; }

        public string Title { get; set; }

        #region Obsolete

        [Obsolete]
        public string Class { get; set; }

        [Obsolete]
        public string IP { get; set; }

        #endregion
    }

public class SampleAutoQueryDb : QueryDb<MailResponseDetailOrm> { }

public class MyQueryServices : Service
{
    public IAutoQueryDb AutoQuery { get; set; }

    // Override with custom implementation
    public object Any(SampleAutoQueryDb query)
    {
        var q = AutoQuery.CreateQuery(query, base.Request);
        q.Join<MailResponseDetailOrm, MailResponseOrm>((x, y) => x.Email == y.Email)
            // .Select<MailResponseDetailOrm, MailResponseOrm>((x, y) => new { x.ID, y.Email })
            ;
        return AutoQuery.Execute(query, q);
    }
}
2

There are 2 best solutions below

5
mythz On BEST ANSWER

Joins in AutoQuery needs to use OrmLite's Joins Reference conventions and all AutoQuery Services results are returned in a Typed DTO, which by default is the table being queried or you can use the QueryDb<From,Into> base class to return a custom result of columns from multiple joined tables.

You would need to use a Custom AutoQuery Implementation or your own Service implementation if you need customizations beyond this, e.g:

public class SampleAutoQueryDb : QueryDb<MailResponseDetailOrm> { }

public class MyQueryServices : Service
{
    public IAutoQueryDb AutoQuery { get; set; }

    // Override with custom implementation
    public object Any(SampleAutoQueryDb query)
    {
        var q = AutoQuery.CreateQuery(query, base.Request);
        q.Join<MailResponseDetailOrm,MailResponseOrm>((x, y) => x.Email == y.Email);
        return AutoQuery.Execute(query, q);
    }
}
1
Bob K On

// The query to join 2 objects on field names not specifically set in the class.

var q = Db.From<MailResponseDetailOrm>().Join<MailResponseDetailOrm>(x,y) => x.Email = y.Email);

// Run the query

var results = Db.Select(q);