开发者

how do i build dynamic linq queries at runtime using strings?

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

i tried

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

and it doesn't work

The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cann开发者_如何学JAVAot be inferred from the usage. Try specifying the type arguments explicitly.

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

how do i build dynamic linq queries at runtime using strings?


In the linq samples directory there is a cool Dynamic Linq library you can use, Scott Gu has a pretty good blog post on how to use it here.


Yes you can have dynamic linq queries at runtime using strings. You need to use the ObjectQuery class as mentioned here and below is the code snippet to do 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 validate the query against the LINQ model at the runtime and throws exception if it couldn't find some of the properties you are using in the query.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜