在选择器实体框架

时间:2016-03-15 05:52:12

标签: c# entity-framework lambda

我需要在Linq2Entity查询中保持联接,所以我这样做并且有效:

var query = _docDedRepository.Table
    .GroupJoin(_actQueueRepository.Table, row => row.Act.ID, actQueue => actQueue.id_act, (left, rights) => new { left, rights = rights.DefaultIfEmpty() })
    .SelectMany(row => row.rights.Select(aq => new { row.left, Status = aq == null ? ConstValues.Statuses.STATUS_NOT_IN_QUEUE : aq.status }));

我想创建扩展方法来做到更简单:

public static IQueryable<TResult> LeftJoin<TOuter, TInner, TKey, TResult>(
    this IQueryable<TOuter> outer, IQueryable<TInner> inner,
    Expression<Func<TOuter, TKey>> outerKeySelector, 
    Expression<Func<TInner, TKey>> innerKeySelector,
    Func<TOuter, TInner, TResult> resultSelector)
{
    if (outer == null)
        throw new ArgumentException("outer");
    if (inner == null)
        throw new ArgumentException("inner");
    if (outerKeySelector == null)
        throw new ArgumentException("outerKeySelector");
    if (innerKeySelector == null)
        throw new ArgumentException("innerKeySelector");
    if (resultSelector == null)
        throw new ArgumentException("resultSelector");

    return outer
        .GroupJoin(inner, outerKeySelector, innerKeySelector, (left, rights) => Tuple.Create(left, rights.DefaultIfEmpty()))
        .SelectMany(x => x.Item2.Select(right => resultSelector(x.Item1, right)));
}

但我从Entity Framework得到一个错误,即查询不支持Invoke。我可以通过Expression或其他方式来做这件事吗?

0 个答案:

没有答案