Why does EF 5.0 not support this EF 4.x LINQ syntax when compiling to sql?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 00:42:44

The problem is caused by the type returned by your repository; the problem can be reproduced when _repository.Reports is not IQueryable<T>. In that case the Reports is considered as a non-scalar variable; which by the way is not allowed in LINQ. See Referencing Non-Scalar Variables Not Supported

On your question regarding why the second query works, it is basically the following extension method of IQueryable<T> which group joins it with IEnumerable<TInner>.

public static IQueryable<TResult> GroupJoin<TOuter, TInner, TKey, TResult>(
    this IQueryable<TOuter> outer,IEnumerable<TInner> inner,
    Expression<Func<TOuter, TKey>> outerKeySelector,
    Expression<Func<TInner, TKey>> innerKeySelector,
    Expression<Func<TOuter, IEnumerable<TInner>, TResult>> resultSelector)

Which just accepts the expression for the key selectors for both outer and inner(instead of referencing non scalar variables); in which the above constraint doesn't apply.

Note: If _repository.Reports is of IQueryable<T> the first query will work; because EF will build the expression tree correctly and execute the appropriate SQL.

Just for curiosities sake, have you tried converting

from el in _repository.EventLogs
where !_repository.Reports.Any(p => p.EventLogID == el.EventlogID)

to

from el in _repository.EventLogs
where !_repository.Reports.Where(p => p.EventLogID == el.EventlogID).Any();

or

from el in _repository.EventLogs
where !_repository.Reports.Where(p => p.EventLogID == el.EventlogID).Count() > 0;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!