0

検索方法には指定クエリを使用しています。これには、search、orderBy、groupBy が含まれます。Search と Orderby は問題ありませんが、この行の groupBy メソッドで問題が発生しますquery.GroupBy(spec.GroupBy).SelectMany(x => x)

次のコードを参照してください。

 public async Task<PaginatedResult<List<EmployeeTypeResponse>>> Handle(GetAllEmployeeTypesQuery request, CancellationToken cancellationToken)
        {

            var employeeTypesWithPaging = _employeeTypeRepository.FindWithSpecificationPattern(new EmployeeTypeSpecifcation(request.Filter, true), true);

ApplyGroupBy(x => x.ShopId); で ShopId を使用して groupBy を適用したいです。

   public class EmployeeTypeSpecifcation : BaseSpecifcation<EmployeeType>
   {
    public EmployeeTypeSpecifcation()
    {

    }
    public EmployeeTypeSpecifcation(PaginationFilter Filter, bool isPaging)
    {

        if (!string.IsNullOrEmpty(Filter.GroupBy))
        {
            ApplyGroupBy(x => x.ShopId);
        }

        var predicate = PredicateBuilder.True<EmployeeType>();

        if (!string.IsNullOrEmpty(Filter.SearchText))
        {
            predicate = predicate.And(x => x.Name.Contains(Filter.SearchText) || x.Code.Contains(Filter.SearchText));
        }

        if (Filter.ShopId != null)
        {
            predicate = predicate.And(c => c.ShopId == Filter.ShopId);
        }

    }
}

そして、このような BaseSpecifcation クラス

 public class BaseSpecifcation<T> : ISpecification<T>
{
    public BaseSpecifcation()
    {
    }
    public BaseSpecifcation(Expression<Func<T, bool>> criteria)
    {
        Criteria = criteria;
    }

    public Expression<Func<T, object>> OrderBy { get; private set; }
    public Expression<Func<T, object>> OrderByDescending { get; private set; }
    public Expression<Func<T, object>> GroupBy { get; private set; }

    protected virtual void ApplyGroupBy(Expression<Func<T, object>> groupByExpression)
    {
        GroupBy = groupByExpression;
    }
    protected void AddOrderBy(Expression<Func<T, object>> orderByExpression)
    {
        OrderBy = orderByExpression;
    }
    protected void AddOrderByDescending(Expression<Func<T, object>> orderByDescExpression)
    {
        OrderByDescending = orderByDescExpression;
    }
    
}

FindWithSpecificationPattern および GetQuery メソッド:

 public IEnumerable<T> FindWithSpecificationPattern(ISpecification<T> specification = null, bool isPaging = false)
    {         
        return  SpecificationEvaluator<T>.GetQuery(DbContext.Set<T>().AsQueryable(), specification, isPaging);
    }

   public class SpecificationEvaluator<TEntity> where TEntity : class
{
    public static IQueryable<TEntity> GetQuery(IQueryable<TEntity> inputQuery, ISpecification<TEntity> spec, bool isPaging)
    {
        var query = inputQuery;

        if (spec.Criteria != null)
        {
            query = query.Where(spec.Criteria);
        }

        if (spec.GroupBy != null)
        {
            query = query.GroupBy(spec.GroupBy).SelectMany(x => x);
        }
    }
}

この行で問題が発生しました

クエリ = query.GroupBy(spec.GroupBy).SelectMany(x => x);

例外はスローしませんが、結果を取得できません。デバッグすると、エラー メッセージが表示されます

Query='((Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable<Nail.Management.Domain.Entities.Catalog.EmployeeType>)query).DebugView.Query' は、タイプ 'System.InvalidOperationException' の例外をスローしました

Google で何度も検索しましたが、指定パターンで GroupBy を実装する例が見つかりません。助けてください、どうもありがとう!

4

0 に答える 0