?
Solved

Sort a Linq query

Posted on 2014-09-15
6
Medium Priority
?
180 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 2000 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 64

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

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

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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

719 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