How do I create dynamic linq queries at runtime using strings?

This article talks about building dynamic queries using strings, is this possible?

I tried

var ase = a.Select("NEW(activity_date as date)"); 

and does not work

Type arguments for the method 'System.Linq.Enumerable.Select (System.Collections.Generic.IEnumerable, System.Func)' cannot be decommissioned. Try to set the type arguments explicitly.

 C:\.....\filename.xaml.cs 

How do I create dynamic linq queries at runtime using strings?

+4
source share
2 answers

There is a cool Dynamic Linq library in the linq sample directory that you can use, Scott Gu has a pretty good blog post on how to use it here.

+3
source

Yes, you can have dynamic linq queries at runtime using strings. You need to use the ObjectQuery class, as indicated here , and below is the code snippet for this:

 string queryString = @"SELECT VALUE product FROM AdventureWorksEntities.Products AS product"; // Call the constructor with the specified query and the ObjectContext. ObjectQuery<Product> productQuery2 = new ObjectQuery<Product>(queryString, context); foreach (Product result in productQuery2) Console.WriteLine("Product Name: {0}", result.Name); 

ObjectQuery will check the query for the LINQ model at run time and throw an exception if it cannot find some properties that you use in the query.

+2
source

All Articles