【发布时间】:2020-07-16 05:09:17
【问题描述】:
Net 核心、Ef 核心和 linq。我有列状态表。它保存值 New、In-Progress 和 Closed。当我将使用 New 查询所有行时,应该首先出现,然后是进行中并关闭。下面是我的代码。
var Requests = await this.RequestRepository.GetAsync(x => x.IsActive == true && x.CreatedBy == LoggedInUser, null, x => x.Country).ConfigureAwait(false);
下面是我的 GetAsync 方法
public async Task<IEnumerable<T>> GetAsync(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, params Expression<Func<T, object>>[] includes)
{
IQueryable<T> query = this.dbSet;
foreach (Expression<Func<T, object>> include in includes)
{
query = query.Include(include);
}
if (filter != null)
{
query = query.Where(filter);
}
if (orderBy != null)
{
query = orderBy(query);
}
return await query.ToListAsync().ConfigureAwait(false);
}
所以我希望所有状态为 New 的行应该首先出现,然后是 In-Progress 并关闭。无论如何,我都找不到解决办法。有人可以帮我写这个。任何帮助,将不胜感激。谢谢
【问题讨论】:
-
IQueryable实现了IEnumerable,那么是什么阻止您使用 LINQ 中的OrderBy方法? -
您好,我需要订购 New、InProgress 和已关闭的订单,我该怎么做?
-
您将
null传递给orderBy那么您为什么期望结果是有序的? -
我不太确定拥有这种“帮助”方法能给你带来什么,而不是直接使用 LINQ - 你为什么要使用它?以使您的代码可读为代价来节省一些击键?
标签: c# linq asp.net-core ef-core-2.0