Creating linq queries during runtime

Posts   
 
    
gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 29-Oct-2008 23:35:47   

Hi,

We have an ASP.NET framework which includes what we call CollectionBasePage's. If a page is inherited from this page you just set the property EntityName (string) and it will use some reflection and invoking to create a EntityCollection filled with all entities for the entityname specified. These pages are in a seperate framework, so we have no access at all to the LLBLGen generated code, this is why we use invocation on the DLL of the data project.

We also keep a, for simplification say, List<string> which contains the fields to display. This could for example contain:

{ "Numer", "City", "CountryEntity.Name" }.

We now want to do the exact samething with LINQ / Queryable datasources (for more efficient use with ASPxGridView in Server Mode, also for retrieving related fields like CountryEntity.Name in this example).

At the moment we have no clue how to create a query like this in runtime:


        var q = from customer in metaData.Customer
                select new
                {                   
                    customer.Number,
                    customer.City,
                    customer.CountryEntity.Name             
                };

Again: This is in a framework which is 100% seperated from the data project, therefore we can only use invoking/activion on the DLL and have no access to the LLBLGen generated code. The ORMSupportClasses are referenced in this framework, so we can for example use IEntity.

We would really appericiate any suggestions on this.

Thanks, Gab

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39861
Joined: 17-Aug-2003
# Posted on: 30-Oct-2008 10:13:22   

The problem is that if the query is written in C#, you'll get expression tree building code, which is required. If you want to do this totally dynamically, you therefore have to build that expression tree exactly like expected in code, which is very very complex and also will make your code unmaintainable.

You could create a separate project which does reference the entities and which has classes which have methods which return an IQueryable<>, and in your llblgen pro unaware main project you refer to that project (e.g. through interfaces) and get the data. That will make life much easier for you, as you can write the queries in plain C#.

Otherwise it's not doable.

Frans Bouma | Lead developer LLBLGen Pro
gabrielk avatar
gabrielk
User
Posts: 231
Joined: 01-Feb-2005
# Posted on: 30-Oct-2008 11:36:25   

(sorry 1: don't know if I should have opened a new thread for this, if so, I am sorry) (sorry 2: we're justing getting start with using LINQ with LLBLGen and therefore need some starting points (after mastering the Predicate system we are now newbies again wink )

I think we can solve this by creating some sort of Provider interface, which will be set during runtime. This provider can then just have access to the LLBLGen code.

The only thing we need to do is to dynamicly set the (joined) fields for a query to a certain entity. Which entity and which fields is determined by strings.


        var q = from c in metaData.Customer
                select new
                {
                    // this should be dynamic.
                };

Could someone give a brief example (when having access to a LLBLGen data project) of how to do this? I've seen a lot of information about dynamicly creating the predicates, but we need to set the columns only.

Thanks again, Gab