【发布时间】:2011-06-16 04:55:46
【问题描述】:
我正在尝试使用此处发布的 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,它会完美运行。
我在这里错过了什么?
【问题讨论】:
-
@Chris:不,我使用的是 edmx 文件。
-
你需要使用“动态”
Select()吗?可以 - 更容易,甚至 - 将动态Where()与静态类型的Select()一起使用。 -
我需要动态选择,因为我想明确选择在运行时返回哪些列。
标签: linq dynamic linq-to-entities