Solved

Sort a Linq query

Posted on 2014-09-15
6
166 Views
Last Modified: 2016-02-15
Experts, I am trying to figure out how to order this query Descending. Please help...
        Dim query = From company In dt.AsEnumerable
                    Select company.Field(Of String)("Company")
                    Distinct

Open in new window

0
Comment
Question by:Basicfarmer
  • 2
  • 2
  • 2
6 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40324508
Dim query = From company In dt.AsEnumerable
            Order By company.Field(Of String)("Company") Descending
            Select company.Field(Of String)("Company")
            Distinct

Open in new window

0
 

Author Closing Comment

by:Basicfarmer
ID: 40324511
Thanks, i was doing that but after the select and it wouldn't work like that.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40324527
Hi Basicfarmer;

The Distinct method call returns an unordered collection and so if you attempted to do it before the Distinct call it would not work. You need to do the ordering after the call to Distinct as shown below.

Dim query = (From company In dt.AsEnumerable
             Select company.Field(Of String)("Company")
             Distinct).OrderByDescending(Function(c) c) 

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Basicfarmer
ID: 40325801
Fernando, thanks for the comment.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40325844
Hi Basicfarmer;

Not a problem. My statement about ordering came from Microsoft documentation [here] and full statement below.

The Distinct(Of TSource)(IEnumerable(Of TSource)) method returns an unordered sequence that contains no duplicate values. It uses the default equality comparer, Default, to compare values.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40325995
@Fernando

That's for Distinct itself, though. If you're already dealing with an ordered collection, I doubt that Distinct is going to re-order the distinct result differently than the ordered source. I'd have to test it, though. If anything, what I am suggesting would be an implementation detail, and the most guaranteed way to ensure an ordered result would be to order after the Distinct, as you mentioned.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now