我的代码中有:
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 to Entities 不识别方法 'Boolean 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));
这个方法运行正常,有没有一种方法可以在查询中使用它?
bool RecordExists<T>(this IEnumerable<T> entity, ...
代替 ;). - shA.t