那是因为你
Compile Method
在你的表情上,你不需要。你应该过去
repBase.GetLmbLang()
作为Where方法的谓词,如下所示:
SupType = model.SupplierType.SupplierTypeTexts.Where(repBase.GetLmbLang())
我想你有点误解了什么时候该用
Expression<Func<SupplierTypeText, bool>>
Func<SupplierTypeText, bool>
.
在哪里?
方法打开
model.SupplierType.SupplierTypeTexts
你在打电话
Enumerable.Where
具有此签名的方法(LINQ to Objects):
public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate
)
但是,通过提供
你的意思是
Queryable.Where
具有此签名(对实体的LINQ):
public static IQueryable<TSource> Where(
this IQueryable<TSource> source,
Expression<Func<TSource, bool>> predicate
)
现在发生的事情是当你编码
model.SupplierType.SupplierTypeTexts.Where(repBase.GetLmbLang())
供应商文本
第一种解决方案
GetLmbLang()
相应的方法:
public Func<SupplierTypeText, bool> GetLmbLang() {
return (p => p.LangID == 1);
}
然后在你的匿名班级里这样称呼它:
SupType=model.SupplierType.SupplierTypeTexts.Where(repBase.GetLmbLang())
你的
是为了保持
GetLmbLang()
支持
:
var ViewModel = _db.Suppliers.Select(model => new {
model,
SupType = _db.SupplierTypeTexts
.Where(repBase.GetLmbLang())
});
:
只有在使用匿名类型进行投影并传递
Expression<Func<...>>
内部代码如下:
var ViewModel = _db.Suppliers.Select(model => new {
SupType = _db.SupplierTypeTexts.Where(repBase.GetLmbLang())
});
如果删除匿名类型的投影,或者保留投影并删除返回的方法
它会消失的。为什么会这样?我不知道,这听起来像是一个提供者的错误。但有一点是肯定的,理论上它应该是可行的,这段代码绝对没有问题。话虽如此,你可以自由地开始一个新的问题,看看其他人有什么要说的。
如果你想这样做,那么你的问题标题应该是这样的:
Why am I getting Provider Error 1205 exception when I try to do a projection with anonymous type while passing in an Expression<Func<...>>?