Aracılığıyla paylaş


Verileri Sıralama

Sıralama işlemi bir veya daha fazla özelliklere göre bir dizi öğeleri sıralar.İlk sıralama ölçütü öğeleri üzerinde birincil sıralama gerçekleştirir.İkinci sıralama ölçütünü belirterek, her birincil sıralama gruptaki öğeleri sıralayabilirsiniz.

Karakter dizisi üzerinde alfabetik sıralama işlemin sonuçları aşağıda gösterilmiştir.

LINQ Sıralama işlemi

Verileri sıralamak standart sorgu işleci yöntemleri aşağıdaki bölümünde listelenir.

Yöntemler

Yöntem adı

Description

C# sorgu ifade sözdizimi

Visual BasicSorgu ifade sözdizimi

Daha Fazla Bilgi

OrderBy

Değerler artan sırada sıralar.

orderby

Order By

Enumerable.OrderBy``2

Queryable.OrderBy``2

OrderByDescending

Değerler azalan düzende sıralar.

orderby … descending

Order By … Descending

Enumerable.OrderByDescending``2

Queryable.OrderByDescending``2

ThenBy

İkincil sıralama artan sırada gerçekleştirir.

orderby …, …

Order By …, …

Enumerable.ThenBy``2

Queryable.ThenBy``2

ThenByDescending

İkincil sıralama azalan sırada gerçekleştirir.

orderby …, … descending

Order By …, … Descending

Enumerable.ThenByDescending``2

Queryable.ThenByDescending``2

Ters Çevir

Bir koleksiyondaki öğelerin sırasını ters çevirir.

Yoktur.

Yoktur.

Enumerable.Reverse``1

Queryable.Reverse``1

Sorgu ifade sözdizimi örnekleri

Birincil sıralama örnekleri

Birincil artan sıralama

Aşağıdaki örnek, nasıl kullanılacağını gösterir orderby (Order By Visual Basic) dizisindeki dizelerin dize uzunluğu, artan sırada sıralamak için LINQ sorgu yan tümcesinde.

Dim words = {"the", "quick", "brown", "fox", "jumps"}

        Dim sortQuery = From word In words 
                        Order By word.Length 
                        Select word

        Dim sb As New System.Text.StringBuilder()
        For Each str As String In sortQuery
            sb.AppendLine(str)
        Next 

        ' Display the results.
        MsgBox(sb.ToString())

        ' This code produces the following output: 

        ' the 
        ' fox 
        ' quick 
        ' brown 
        ' jumps
string[] words = { "the", "quick", "brown", "fox", "jumps" };

            IEnumerable<string> query = from word in words
                                        orderby word.Length
                                        select word;

            foreach (string str in query)
                Console.WriteLine(str);

            /* This code produces the following output:

                the
                fox
                quick
                brown
                jumps
            */

Birincil azalan sıralama

Bir sonraki örnek, nasıl kullanılacağını göstermektedir orderbydescending (Order By Descending Visual Basic) dizeleri azalan sırada kendi ilk harfine göre sıralamak için LINQ sorgu yan tümcesinde.

Dim words = {"the", "quick", "brown", "fox", "jumps"}

        Dim sortQuery = From word In words 
                        Order By word.Substring(0, 1) Descending 
                        Select word

        Dim sb As New System.Text.StringBuilder()
        For Each str As String In sortQuery
            sb.AppendLine(str)
        Next 

        ' Display the results.
        MsgBox(sb.ToString())

        ' This code produces the following output: 

        ' the 
        ' quick 
        ' jumps 
        ' fox 
        ' brown
string[] words = { "the", "quick", "brown", "fox", "jumps" };

            IEnumerable<string> query = from word in words
                                        orderby word.Substring(0, 1) descending 
                                        select word;

            foreach (string str in query)
                Console.WriteLine(str);

            /* This code produces the following output:

                the
                quick
                jumps
                fox
                brown
            */

İkincil sıralama örnekleri

İkincil artan sıralama

Aşağıdaki örnek, nasıl kullanılacağını gösterir orderby (Order By Visual Basic) birincil ve ikincil sıralama dizelerin bir dizide gerçekleştirmek için LINQ sorgu yan tümcesinde.Dizeleri, öncelikle uzunluğuna göre ve da ürüne dizenin hem de artan ilk harfine göre sıralanır.

Dim words = {"the", "quick", "brown", "fox", "jumps"}

        Dim sortQuery = From word In words 
                        Order By word.Length, word.Substring(0, 1) 
                        Select word

        Dim sb As New System.Text.StringBuilder()
        For Each str As String In sortQuery
            sb.AppendLine(str)
        Next 

        ' Display the results.
        MsgBox(sb.ToString())

        ' This code produces the following output: 

        ' fox 
        ' the 
        ' brown 
        ' jumps 
        ' quick
string[] words = { "the", "quick", "brown", "fox", "jumps" };

            IEnumerable<string> query = from word in words
                                        orderby word.Length, word.Substring(0, 1)
                                        select word;

            foreach (string str in query)
                Console.WriteLine(str);

            /* This code produces the following output:

                fox
                the
                brown
                jumps
                quick
            */

İkincil azalan sıralama

Bir sonraki örnek, nasıl kullanılacağını göstermektedir orderbydescending (Order By Descending Visual Basic) bir birincil sıralama düzeni ve azalan bir ikincil sıralamayı artan gerçekleştirmek için LINQ sorgu yan tümcesinde.Dizeler, öncelikle uzunluğuna göre ve da ürüne dizenin ilk harfine göre sıralanır.

Dim words = {"the", "quick", "brown", "fox", "jumps"}

        Dim sortQuery = From word In words 
                        Order By word.Length, word.Substring(0, 1) Descending 
                        Select word

        Dim sb As New System.Text.StringBuilder()
        For Each str As String In sortQuery
            sb.AppendLine(str)
        Next 

        ' Display the results.
        MsgBox(sb.ToString())

        ' This code produces the following output: 

        ' fox 
        ' the 
        ' quick 
        ' jumps 
        ' brown
string[] words = { "the", "quick", "brown", "fox", "jumps" };

            IEnumerable<string> query = from word in words
                                        orderby word.Length, word.Substring(0, 1) descending 
                                        select word;

            foreach (string str in query)
                Console.WriteLine(str);

            /* This code produces the following output:

                the
                fox
                quick
                jumps
                brown
            */

Ayrıca bkz.

Görevler

Nasıl yapılır: Join Tümcesinin Sonuçlarını Sıralama (C# Programlama Kılavuzu)

Nasıl yapılır: Sorgu Sonuçlarını LINQ Kullanarak Sıralama (Visual Basic)

Nasıl yapılır: Herhangi bir Sözcük veya Alana Göre Metin Verilerini Sıralama veya Filtreleme (LINQ)

Başvuru

orderby tümcesi (C# Başvurusu)

Order By Tümcesi (Visual Basic)

System.Linq

Kavramlar

Standart Sorgu İşleçlerine Genel Bakış