views:

37

answers:

0

I am trying to figure out how to create some dynamic queries. I have looked at Linq.Dyanmic and it is missing some things that I think I may need.

My goal is to essential flatten the relational database, this is for creating searches on the fly by the user, into one object with properties from many different tables/entities.

Example:

var conaddplusadd = man.ContactAddresses
            .GroupJoin(man.Addresses,
                conadd => conadd.AddressId, add => add.AddressId,
                (conadd, add) => new { ConAddress = conadd, MyAddress = add })
            .SelectMany(myadd => myadd.MyAddress.DefaultIfEmpty<Address>(),
                (myadd, add) => new
                {
                    ContactId = myadd.ConAddress.ContactId,
                    Preferred = myadd.ConAddress.Preferred,
                    Line1 = add.Line1,
                    Line2 = add.Line2,
                    City = add.City,
                    State = add.State,
                    Zip = add.Zip
                });

Func conidf = con => con.ContactId; //Just looking into options for solving this issue

        var conaddlingmethod = man.Contacts
            .GroupJoin(conaddplusadd, 
                conidf, conadd => conadd.ContactId,
                (con, conadd) => new { Contact = con, ConAddresses = conadd }) //Join
            .SelectMany(myconadd => myconadd.ConAddresses.DefaultIfEmpty(),
                (myconadd, conadd) => new
                {
                    ContactId = myconadd.Contact.ContactId,                        
                    FirstName = myconadd.Contact.FirstName,
                    LastName = myconadd.Contact.LastName,              
                    Preferred = (conadd == null) ? false : conadd.Preferred ?? false,
                    Line1 = (conadd == null) ? null : conadd.Line1
                })//flatten                   
                ;

I believe the best scenario is to recurrsively build the group by calling a method from a child class to build its group join and returning its results.

The problem is creating the output object. I don't know before hand what the outputed properties should be. I looked at this example for the SelectMany addition to Linq.Dynamic but it doesn't account for AnonymousType (s).

I am in the begining stages of this and haven't done any real complex Linq algo's before. Any help or guidence would be greatly appreciated.