在我的代码有:LINQ到实体自定义的方法
context.TableA
.Where(x =>
x.Created >= startDate
&& context.TableB.RecordExists(x.Id, 1));
而且RecordExists的定义如下:
public static bool RecordExists(this IQueryable<TableB> entity, int entityId, int entityTypeId)
{
return entity.Any(x => x.EntityId == entityId && x.EntityTypeId == entityTypeId);
}
上述调用失败
NotSupportedException异常:LINQ到实体不识别方法 '布尔RecordExists(System.Linq.IQueryable`1 [TableB],Int32,Int32)' 方法,并且此方法无法转换为商店表达式。
但如果我更改查询:
context.TableA
.Where(x =>
x.Created >= startDate
&& context.TableB.Any(p => p.EntityId == x.Id && p.EntityTypeId == 1));
它工作得很好,有没有在查询中使用这个方法的方法吗?
可惜没有办法在Linq to Entities中使用扩展方法,因为EF不支持此功能。 – Vladimir
我想你可以使用'bool RecordExists(这个IEnumerable 实体,...'而不是;)。 –
@ shA.t为什么会这样? – Medo