这有效:
Entities.WorkOrderSet.Where(MyCustomMethod);
Run Code Online (Sandbox Code Playgroud)
这不是:
Entities.WorkOrderSet.Where(o => MyCustomMethod(o));
Run Code Online (Sandbox Code Playgroud)
([编辑]即使没有new,它也不起作用)
我理解为什么第二个不起作用 - 但为什么世界上第一个工作!? 我不应该在运行时获得"LINQ-to-Entities无法识别方法...",就像第二个一样?
作为参考,这是MyCustomMethod
public bool MyCustomMethod(WorkOrder workOrder)
{
return !workOrder.WorkOrderNum.StartsWith("A", StringComparison.CurrentCultureIgnoreCase);
}
Run Code Online (Sandbox Code Playgroud)
使用EF1,而不是EF4
我有一个方法:
public static void GetObjects()
{
using(MyContext context = new MyContext())
{
var objects = context.Bars.Where(b => b.Prop1 != null)
.Select(b => new MyObject{Prop = b.Prop1, Name = b.Name})
.ToList();
foreach(var object in objects)
{
// do something with the object
}
}
}
Run Code Online (Sandbox Code Playgroud)
我重构了方法,使其更通用,以便我可以传入一个,Func以便我可以指定where语句,并将Bars表中的属性分配给MyObject.Prop这样:
public static void GetObjectsV2(Func<Bar, bool> whereFunc, Func<Bar, string> selectPropFunc)
{
using(MyContext context = new MyContext())
{
var objects = context.Bars.Where(whereFunc)
.Select(b => new MyObject{Prop = selectPropFunc(b), Name …Run Code Online (Sandbox Code Playgroud)