Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架-如何将Include传递给方法调用方?_Entity Framework - Fatal编程技术网

Entity framework 实体框架-如何将Include传递给方法调用方?

Entity framework 实体框架-如何将Include传递给方法调用方?,entity-framework,Entity Framework,我有一个从实体框架数据库调用DbSet的方法: public static List<CostEntryVM> ToViewModelList(this DbSet<CostEntry> CostEntrys, Expression<Func<CostEntry, bool>> query) { return AutoMapper.Mapper.Map<List<CostEntry>, List<C

我有一个从实体框架数据库调用DbSet的方法:

    public static List<CostEntryVM> ToViewModelList(this DbSet<CostEntry> CostEntrys, Expression<Func<CostEntry, bool>> query) {

        return AutoMapper.Mapper.Map<List<CostEntry>, List<CostEntryVM>>(
              CostEntrys
                .Include(x => x.Job)
                .Include(x => x.User)
                .Where(query)
                .ToList());
    }
我还想打电话:

        CostEntrys.ToViewModelList(x => x.Include(y => y.Job).Include(y.User), x => x.Active == true);
在我的一生中,我无法弄清楚方法签名应该是什么样子,或者如何将其应用于DbSet


如何执行此操作?

首先,您需要将扩展方法更改为:

public static List<CostEntryVM> ToViewModelList(
        this DbSet<CostEntry> CostEntrys, 
        Expression<Func<CostEntry, bool>> query,
        Func<IQueryable<CostEntry>, IQueryable<CostEntry>> func)
{
    // Adding the predicate query
    IQueryable<CostEntry> queryable = CostEntrys.Where(query);

    // Adding include paths
    IQueryable<CostEntry> queryableWithFetch = func(queryable);

    // Executing the query and map it to the view model object
    return AutoMapper.Mapper.Map<List<CostEntry>, List<CostEntryVM>>(
            queryableWithFetch.ToList());
}
public static List<CostEntryVM> ToViewModelList(
        this DbSet<CostEntry> CostEntrys, 
        Expression<Func<CostEntry, bool>> query,
        Func<IQueryable<CostEntry>, IQueryable<CostEntry>> func)
{
    // Adding the predicate query
    IQueryable<CostEntry> queryable = CostEntrys.Where(query);

    // Adding include paths
    IQueryable<CostEntry> queryableWithFetch = func(queryable);

    // Executing the query and map it to the view model object
    return AutoMapper.Mapper.Map<List<CostEntry>, List<CostEntryVM>>(
            queryableWithFetch.ToList());
}
CostEntrys.ToViewModelList(
        x => x.Active == true, 
        x => x.Include(y => y.Job).Include(y.User));