简体   繁体   中英

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)' cannot 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?

在linq samples目录中有一个很酷的Dynamic Linq库你可以使用,Scott Gu有一篇关于如何在这里使用它的相当不错的博客文章

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM