【发布时间】:2014-10-08 11:03:21
【问题描述】:
我正在尝试在我的应用程序中进行一些代码优化,并用一个方法替换几个做同样事情的方法。 我有这个 linq,我需要使用各种过滤器来执行,但我在 .Where(filter) 部分遇到设计时错误
Expression<Func<dbTable, bool>> filter;
switch (i)
{
case 1: filter = (p => p.f1 == ExternalParam);
case 2: filter = (p => p.f2 == ExternalParam);
}
var ds = (from tbl in dbEntities.dbTable
orderby tbl.f1
select new
{
f1 = tbl.f1,
f2 = tbl.f2,
f3 = tbl.f3,
f4 = tbl.f4,
}
).Where(filter);
错误是
Error 1 'System.Linq.IQueryable<AnonymousType#1>' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Queryable.Where<TSource>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource,int,bool>>)' has some invalid arguments
Error 2 Argument 2: cannot convert from 'System.Linq.Expressions.Expression<System.Func<AppNameSpace.dbTable,bool>>' to 'System.Linq.Expressions.Expression<System.Func<AnonymousType#1,int,bool>>'
有人可以帮忙吗?
【问题讨论】:
-
我认为 (from..orderby..select) 周围不应该有圆括号
标签: c# .net linq linq-to-objects