How can I sort by multiple fields with LINQ?

Can someone help by showing me how to sort a LINQ expression.

I have the following:

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

           })

I would like to do the following:

1) The first four characters or the RowKey field
2) ShortTitle

I am not sure how to make the appearance on several characters, and also not sure how to make the background look.

+5
source share
5 answers

For the first four characters, include this in your existing statement, then add a ShortTitle afterwards

.OrderByDescending(item => item.RowKey.Substring(0,4))
.ThenBy(item => item.ShortTitle)
+8
source

You can use 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 tobi

+1
source
+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,

           })
+1

, , Substring :

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

( - .)

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,

       }) 
+1

All Articles