Solved

Sort a Linq query

Posted on 2014-09-15
6
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 63

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This video teaches viewers about errors in exception handling.

738 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