gpt4 book ai didi

linq - 如何使用linq按多个字段排序?

转载 作者:行者123 更新时间:2023-12-03 11:50:20 26 4
gpt4 key购买 nike

我正在创建一个模拟数据源,希望能够将其传递给SortExpressions列表。

public SortExpression(string name, SortDirection direction)
{
this.name = name;
this.direction = direction;
}

用Jon Skeet的代码以及整个类更新。 GetData()只是用x条记录填充对象。
public class Data
{

public int Id { get; set; }
public Guid gId { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public DateTime Created { get; set; }
public string SortMe { get; set; }
public static List<Data> GetFakeData(int start, int numberToFetch, IList<SortExpression> sortExpressions, IList<FilterExpression> filterExpressions, out int totalRecords)
{
DataCollection items = GetData();
IEnumerable<Data> query = from item in items select item;

bool sortExpressionsExist = sortExpressions != null;
if (sortExpressionsExist)
{
// Won't be read in the first iteration; will be written to
IOrderedEnumerable<Data> orderedQuery = null;
for (int i = 0; i < sortExpressions.Count; i++)
{
// Avoid single variable being captured: capture one per iteration.
// Evil bug which would be really hard to find :)
int copyOfI = i;
// Tailor "object" depending on what GetProperty returns.
Func<Data, object> expression = item =>
item.GetType().GetProperty(sortExpressions[copyOfI].Name);

if (sortExpressions[i].Direction == SortDirection.Ascending)
{
orderedQuery = (i == 0) ? query.OrderBy(expression)
: orderedQuery.ThenBy(expression);
}
else
{
orderedQuery = (i == 0) ? query.OrderByDescending(expression)
: orderedQuery.ThenByDescending(expression);
}
}
query = orderedQuery;
}

bool filterExpressionsExist = filterExpressions != null;
if (filterExpressionsExist)
{
foreach (var filterExpression in filterExpressions)
{
query.Where(item => item.GetType().GetProperty(filterExpression.ColumnName).GetValue(item, null).ToString().Contains(filterExpression.Text));
}
}
totalRecords = query.Count();


return query.Skip(start).Take(numberToFetch).ToList<Data>();
}
}

似乎什么也没做。编译,没有错误,没有排序。有任何想法吗?

最佳答案

有两个问题。第一个是其他人提到的-您需要使用OrderBy等返回的值。第二个是每次调用OrderBy时,都会添加一个新的“主”顺序。在第一次订购后,您真的想要ThenBy。不幸的是,这使它变得非常丑陋。重构后仍然很丑陋,但还算不错。

IEnumerable<Data> query = from item in items select item;
if (sortExpressionsExist)
{
// Won't be read in the first iteration; will be written to
IOrderedEnumerable<Data> orderedQuery = null;
for (int i = 0; i < sortExpressions.Count; i++)
{
// Avoid single variable being captured: capture one per iteration.
// Evil bug which would be really hard to find :)
int copyOfI = i;
// Tailor "object" depending on what GetProperty returns.
Func<Data, object> expression = item =>
item.GetType()
.GetProperty(sortExpressions[copyOfI].Name)
.GetValue(item, null);

if (sortExpressions[i].Direction == SortDirection.Ascending)
{
orderedQuery = (i == 0) ? query.OrderBy(expression)
: orderedQuery.ThenBy(expression);
}
else
{
orderedQuery = (i == 0) ? query.OrderByDescending(expression)
: orderedQuery.ThenByDescending(expression);
}
}
query = orderedQuery;
}

关于linq - 如何使用linq按多个字段排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/958220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com