LINQ Generic GroupBy和SelectBy with Method Syntax

时间:2017-02-05 14:38:39

标签: entity-framework linq select group-by linq-method-syntax

我试图让它变得通用但没有成功。它没有得到相关的用户实体。

  return from transactions in context.Transaction
         where transactions.Date.Month == date.Month && transactions.Date.Year == date.Year
         join users in context.UserProfile on transactions.UserID equals users.UserID
         orderby transactions.MonthlyTotalExperiencePoint
         group transactions by transactions.UserID into groupedList
         select groupedList.First()).Take(50).ToList();

我试过了:

public async Task<List<object>> GetAllGroup<TReturn, TOrderKey, TGroupKey>(Expression<Func<IGrouping<TGroupKey, TEntity>, TReturn>> selectExp,
                                                           Expression<Func<TEntity, bool>> whereExp,
                                                           Expression<Func<TEntity, TOrderKey>> orderbyExp,
                                                           bool descending,
                                                           int top,
                                                           Expression<Func<TEntity, TGroupKey>> groupByExp,
                                                           params Expression<Func<TEntity, object>>[] includeExps)
    {
        var query = DbSet.Where(whereExp);
        query = !descending ? query.OrderBy(orderbyExp) : query.OrderByDescending(orderbyExp);
        if (includeExps != null)
            query = includeExps.Aggregate(query, (current, exp) => current.Include(exp));
        return await query.GroupBy(groupByExp).Select(selectExp).Take(top).ToListAsync();
    }

它在使用时出错:

 var item = await transactionRepository.GetAllGroup(x => x.FirstOrDefault(), x => x.Date != null, x => x.MonthlyTotalExperiencePoint, true, 50, x => x.MonthlyTotalExperiencePoint, x => x.User);

0 个答案:

没有答案