gpt4 book ai didi

c# - LINQ 多重排序依据

转载 作者:太空狗 更新时间:2023-10-30 00:21:59 26 4
gpt4 key购买 nike

我创建了一个具有以下参数的函数:

List<Expression<Func<CatalogProduct, bool>>> orderBy = null

这个参数是可选的,如果它被填写它应该为我创建一个订单,而不是通过构造,这样我就可以在 SQL 服务器上订购结果。

我试过:

            IOrderedQueryable temp = null;
foreach (Expression<Func<CatalogProduct, bool>> func in orderBy)
{
if (temp == null)
{
temp = catalogProducts.OrderBy(func);
}
else
{
temp = temp.ThanBy(func);
}
}

但是比 By 没有被重新识别。有人知道我该如何解决这个问题吗?


我将其更改为 .ThenBy() 但这只允许在 .OrderBy() 之后直接使用,而不是在 IOrderedQueryable 上使用

所以 temp = catalogProducts.OrderBy(func).ThenBy(func);允许但 temp = catalogProducts.OrderBy(func); temp = temp.ThenBy(func);不是

还有什么建议吗?

最佳答案

两个问题;首先,ThanBy应该是 ThenBy ;其次,ThenBy仅适用于通用 类型,IOrderedQueryable<T> .

所以改为:

        IOrderedQueryable<CatalogProduct> temp = null;
foreach (Expression<Func<CatalogProduct, bool>> func in orderBy) {
if (temp == null) {
temp = catalogProducts.OrderBy(func);
} else {
temp = temp.ThenBy(func);
}
}

你应该被分类。

关于c# - LINQ 多重排序依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3084671/

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