Linq Expression to generic - how

时间:2012-06-01 15:29:32

标签: c# .net linq lambda

我想将其转换为通用类型:

using System.Linq.Expression
using System.Collections.Generic

public IQueryable<T> AllIncluding(
                          params Expression<Func<T, object>>[] includeProperties)
{
    IQueryable<T> query = All;
    foreach (var includeProperty in includeProperties)
    {
        // query = query.Include(includeProperty);
    }
    return query;
}

但它似乎不起作用,我该怎么做?

2 个答案:

答案 0 :(得分:2)

Include方法仅在DbQuery<T>上可用,因此您必须使用直接强制转换或as操作数或使用方法DbExtensions.Include将查询强制转换为该方法:

public IQueryable<T> AllIncluding(
    params Expression<Func<T, object>>[] includeProperties)
{
    IQueryable<T> query = All as DbQuery<T>;
    if (query == null)
    {
       return All;
    }

    foreach (var includeProperty in includeProperties)
    {
        // query = query.Include(includeProperty);
    }
    return query;
}

答案 1 :(得分:1)

System.Data.Entity添加使用;

实体框架4.1包含强类型版Includehttp://msdn.microsoft.com/en-us/library/gg671236(VS.103).aspx

这是一种扩展方法。