2012-06-13 24 views
5

Birisi bana bir LINQ ifadesinin nasıl sıralanacağını göstererek yardımcı olabilir.LINQ ile birden çok alanı nasıl sıralayabilirim?

1) İlk dört karakter veya alan RowKey
2) ShortTitle

: Ne yapmak istiyorum olduğunu
.OrderByDescending(item => item.RowKey) 
.Select((t, index) => new City.Grid() 
      { 
       PartitionKey = t.PartitionKey, 
       RowKey = t.RowKey, 
       Row = index + 1, 
       ShortTitle = t.ShortTitle, 

      }) 

aşağıdakilere bir sıralama yapmak:

aşağıdakileri sorunum

Sadece birkaç karakter üzerinde nasıl bir sıralama yapılacağından emin değilim ve ayrıca nasıl ikincil bir sıralama yapılacağından emin değilim.

+0

.OrderByDescending deneyin (öğe => {item.RowKey, item.ShortTitle}) –

+0

kullanımı ThenBy [yöntem] (http://msdn.microsoft.com/en- tr/kütüphane/bb535112.aspx). –

+0

olası yinelenen [linq içinde 2 alanları ile sipariş nasıl kullanılır?] (Http://stackoverflow.com/questions/1989674/how-to-use-orderby-with-2-fields-in-linq) – V4Vendetta

cevap

8

, mevcut açıklamada, daha sonra eklemesini içerir ShortTitle sonradan

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

Öğenin uzunluğu.RowKey <4 ise bir özel durumun atılacağını unutmayın. – Tobias

1
Sen OrderByDescending (...) kullanabilirsiniz

. 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 ilk dört karakter için

+0

Ama ben sadece şimdi RowKey'in ilk dört karakterini sıralamak istiyorum. – Alan2

+0

item.Rowkey.Substring (0, Math.Min (4, item.RowKey.Length)) – Tobias

1

Sen, ifade ağaca Substring geçebilir bir altkatarıyla sıralama, ilk şartı için Enumerable.ThenBy method

.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

kullanabilirsiniz:

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

(Ama sınırları istisnalar out farkında olmak .) ikinci tür için

kullanımı the ThenBy() method:

0.123.
.ThenBy(item => item.ShortTitle) 

Kombine:

.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, 

     }) 
İlgili konular