5

LINQ式をソートする方法を教えてください。

私は次のものを持っています:

 .OrderByDescending(item => item.RowKey)
 .Select((t, index) => new City.Grid()
           {
               PartitionKey = t.PartitionKey,
               RowKey = t.RowKey,
               Row = index + 1,
               ShortTitle = t.ShortTitle,

           })

私がやりたいことは、次のソートを行うことです。

1) 最初の 4 文字またはフィールドの RowKey
2) ShortTitle

ほんの数文字で並べ替えを行う方法がわかりません。また、二次並べ替えを行う方法もわかりません。

4

5 に答える 5

8

最初の 4 文字については、既存のステートメントに含め、その後に ShortTitle を追加します

.OrderByDescending(item => item.RowKey.Substring(0,4))
.ThenBy(item => item.ShortTitle)
于 2012-06-13T08:42:33.487 に答える
1

Enumerable.ThenBy メソッドを使用できます

.OrderByDescending(item => item.RowKey)
.ThenByDescending(item => item.ShortTitle)
 .Select((t, index) => new City.Grid()
           {
               PartitionKey = t.PartitionKey,
               RowKey = t.RowKey,
               Row = index + 1,
               ShortTitle = t.ShortTitle,

           })
于 2012-06-13T08:44:24.807 に答える
1

Substring最初の要件である部分文字列による並べ替えについては、式ツリーに渡すことができます。

.OrderByDescending(item => item.RowKey.Substring(0, 4))

(ただし、範囲外の例外に注意してください。)

2 番目の並べ替えには、次のThenBy()メソッドを使用します。

.ThenBy(item => item.ShortTitle)

組み合わせ:

.OrderByDescending(item => item.RowKey.Substring(0, 4))
.ThenBy(item => item.ShortTitle)
.Select((t, index) => new City.Grid()
       {
           PartitionKey = t.PartitionKey,
           RowKey = t.RowKey,
           Row = index + 1,
           ShortTitle = t.ShortTitle,

       }) 
于 2012-06-13T08:45:52.447 に答える
1

OrderByDescending(...).ThenBy()... を使用できます。

.OrderByDescending(item => item.RowKey.Substring(0, Math.Min(4, item.RowKey.Length)))
.ThenBy(item => item.ShortTitle)
.Select((t, index) => new City.Grid()
       {
           PartitionKey = t.PartitionKey,
           RowKey = t.RowKey,
           Row = index + 1,
           ShortTitle = t.ShortTitle,

       })

Hth トビ

于 2012-06-13T08:42:13.477 に答える
1

ThenByおよびを使用しThenByDescendingて、並べ替え用の 2 番目のキーを追加できます。

于 2012-06-13T08:42:43.313 に答える