开发者

System.Linq.Dynamic not working for the Entity Framework

I'm trying to use the LINQ Dynamic Query Library posted here - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

It is supposed to work for the EF too but I can't get it to fulfill that claim.

The following works great:

List<string> paramsList = new List<string> {"CustomerID"};
        var customer =
            ctx.Customers.Where(cus=>cus.CompanyName.Contains("A")).Select("new(" +
                                 string.Join(", ", paramsList.ToArray()) +
                                 ")");     

However if I omit the "Where" clause and do something like this

List<string> paramsList = new List<string> {"CustomerID"};
        var customer =
            ctx.Customers.Select("new(" +
                                 string.Join(", ", paramsList.ToArray()) +
                                 ")");     

I get the following error:

'new' cannot be resolved into a valid type constructor or function., near 开发者_如何转开发function, method or type constructor

It works perfectly if I use Linq2Sql instead of Linq2Entities.

What am I missing here?


In case someone else comes across this like I did:

ctx.Customers is an ObjectSet, which does not work with Dynamic Linq.

However, as soon as you throw on something like .Contains(), you get an IQueryable, which does work.

You can also explicity convert to an IQueryable, like this:

ctx.Customers.AsQueryable().Select(...)


Got a reply to my question on the MSDN forum for the Entity Framework -

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/7d29f8d8-875e-47c5-adb2-eb8756be36c1/#cc1ce970-02da-4b9e-8067-c37a33461c8d

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜