且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为多列OrderBy表达式创建表达式树

更新时间:2023-02-16 12:56:10

通过调用 OrderBy(),然后零次或多次调用 ThenBy().您无法通过单次调用 OrderBy()来做到这一点.

Ordering by multiple columns in LINQ works by calling OrderBy() followed by zero or more calls to ThenBy(). You can't do this using a single call to OrderBy().

例如,如果您想按 a b 列进行排序,则必须生成一个类似于以下内容的表达式:

For example, if you can want to sort by the columns a and b, you will have to generate an expression that looks something like:

items.OrderBy(p => p.a).ThenBy(p => p.b)