System.Linq.Dynamic 不适用于实体框架
我正在尝试使用此处发布的 LINQ 动态查询库 - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq -dynamic-query-library.aspx
它应该也适用于 EF,但我无法让它满足该要求。
以下效果很好:
List<string> paramsList = new List<string> {"CustomerID"};
var customer =
ctx.Customers.Where(cus=>cus.CompanyName.Contains("A")).Select("new(" +
string.Join(", ", paramsList.ToArray()) +
")");
但是,如果我省略“Where”子句并执行类似的操作,
List<string> paramsList = new List<string> {"CustomerID"};
var customer =
ctx.Customers.Select("new(" +
string.Join(", ", paramsList.ToArray()) +
")");
则会出现以下错误:
“new”无法解析为有效的类型构造函数或函数。、近函数、方法或类型构造函数
如果我使用 Linq2Sql 而不是 Linq2Entities,它会完美地工作。
我在这里缺少什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果其他人像我一样遇到这种情况:
ctx.Customers 是一个 ObjectSet,它不适用于 Dynamic Linq。
然而,一旦你添加像 .Contains() 这样的东西,你就会得到一个 IQueryable,它确实有效。
您还可以显式转换为 IQueryable,如下所示:
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:
在实体框架的 MSDN 论坛上得到了我的问题的答复 -
http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/7d29f8d8-875e-47c5-adb2-eb8756be36c1 /#cc1ce970-02da-4b9e-8067-c37a33461c8d
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