Solved

Sort a Linq query

Posted on 2014-09-15
6
165 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 74

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 74

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

760 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

21 Experts available now in Live!

Get 1:1 Help Now