如何在存储库模式中实现SelectMany

时间:2017-06-23 19:33:49

标签: generics select repository-pattern

我想在通用存储库模式中实现SelectMany()查询 我的代码如下所示:

 var query = ctx.Storehouse.Where(x => x.Id == getId)
                    .SelectMany(x => x.Products).Select(x => new
                    {
                        x.Id,
                        .
                        ...
                    }).ToList();

我需要更改以下函数的参数:

public virtual async Task<ICollection<TEntity>> ListOfFoo<TResult>(
        Expression<Func<TEntity, bool>> condition01,
        Expression<Func<TEntity, TResult>> condition02,
        Expression<Func<TEntity, TResult>> condition03)
    {
        return await Dbset.Where(condition01).SelectMany(condition02).Select(condition03).ToListAsync();
    }

和获取错误如下所示:

Error When use argument for condition02

1 个答案:

答案 0 :(得分:1)

public Task<ICollection<TResult>> ListOfFooAsync<TEntity1, TEntity2, TResult>(
            Expression<Func<TEntity1, bool>> condition01,
            Expression<Func<TEntity1, IEnumerable<TEntity2>>> condition02,
            Expression<Func<TEntity2, TResult>> condition03) where TEntity1:class
        {
            return _context.Set<TEntity1>()
                           .Where(condition01)
                           .SelectMany(condition02)
                           .Select(condition03)
                           .ToListAsync();
        }

用法:

var list = await ListOfFooAsync<User, Category, CustomType1>(
            user => user.Id > 1, user => user.Categories, category => new CustomType1
            {
                Id = category.Id
            });