This blog is a knowledge base...where I clip cool tricks and urls

Dynamic Query Reflection within Linq

clipped from www.west-wind.com
As I suggested in my comment on the last blog post - read the docs and samples...and failing that ask a question on the LINQ forums: http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=123&SiteID=1

Even Anders hangs out on that forum - so you will get help if you ask a question on it. ;-)

Daniel Moth has a good pointer to some LINQ samples you can use: http://www.danielmoth.com/Blog/2007/08/linq-samples.html One of these LINQ samples is called "DynamicQuery", and it provides a useful library that enables you to write dynamic LINQ expressions that enable you to accomplish what you are after above.

For example, you could write:

var query =
db.Customers.
Where("City = @0 and Orders.Count >= @1", "London", 10).
OrderBy("CompanyName").
Select("new(CompanyName as Name, Phone)");
 blog it

No comments: