Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

specific Order by Linq to SQL

Is possible to order on Linq according to a specific order? something like

List<bbHeader> bb = new List<bbHeader>();
bb.OrderBy(x => x.Country.CompareTo(new string[]{"AR","CL","PY","UY","AUP"}));

The idea is that the Country field is ordered according to the specific order of the string

like image 458
Guillermo Avatar asked Aug 12 '11 18:08

Guillermo


People also ask

How do I get data in descending order in Linq?

OrderByDescending Operator If you want to rearrange or sort the elements of the given sequence or collection in descending order in query syntax, then use descending keyword as shown in below example. And in method syntax, use OrderByDescending () method to sort the elements of the given sequence or collection.

What is OrderByDescending?

OrderByDescending sorts the collection in descending order. OrderByDescending is valid only with the Method syntax. It is not valid in query syntax because the query syntax uses ascending and descending attributes as shown above. Example: OrderByDescending C#

What is Linq in C# with example?

LINQ is the basic C#. It is utilized to recover information from various kinds of sources, for example, XML, docs, collections, ADO.Net DataSet, Web Service, MS SQL Server, and different database servers.


2 Answers

There's a very direct way in your example:

var sequence = new [] { "AR", "CL", "PY", "UY", "AUP" };

List<bbHeader> bb = new List<bbHeadher>();

// fill bb

// takes the item, checks the index of the country in the array
var result = bb.OrderBy(x => Array.IndexOf(sequence, x.Country));

In this way, you are ordering by the index Country is found in the sequence string. Just keep in mind that not-found items will be -1, which you can correct for also if you like.

If you want to do anything more complex, you can to create your own custom IComparer class implementation to compare the items using your custom order. This can then be passed into OrderBy.

Such an IComparer would look like:

public sealed class SequenceComparer : IComparer<string>
{
    private string[] _sequence { get; set; }

    public SequenceComparer(string[] sequence)
    {
        if (sequence == null) throw new ArgumentNullException("sequence");

        _sequence = sequence;
    }

    public int Compare(string x, string y)
    {
        if (ReferenceEquals(x, y)) return 0;

        return Array.IndexOf(_sequence, x).CompareTo(Array.IndexOf(_sequence, y));
    }
}

And can be called like:

var result = bb.OrderBy(x => x.Country, new SequenceComparer(new [] { "AR", "CL", "PY", "UY", "AUP" }));

Either way works well, the latter is nice and reusable, but the former (using IndexOf directly) is still very concise as well. Your choice.

like image 108
James Michael Hare Avatar answered Sep 25 '22 15:09

James Michael Hare


well you can pass in your own delegate to orderby function and the comparison logic in that case can be defined by you.

like image 20
Baz1nga Avatar answered Sep 24 '22 15:09

Baz1nga