Convert string to LINQ query

2019-05-18 14:33发布

问题:

which method or way should be used to convert a string to a linq query ?

Environment: VS 2010/C#

回答1:

I'm not sure of what you are trying to achieve but if you are referring to creating linq queries from strings you could use the dynamic linq library, check it out here http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx



回答2:

You have to do something like this:

var query =
                db.Customers.Where("Country== @0 and Orders.Count >= @1", "Costa Rica", 10).
                OrderBy("CompanyName").
                Select("New(CompanyName as Name, Phone)");

some parts could be taken from strings, some others (tables) cant



回答3:

Converting a string to Linq query isn't directly possibly with out some parsing and translation into System.Linq.Expression objects. Neither is trivial.

Check this out for one example.