Solved

Ordering IEnumberable in VB.Net

Posted on 2014-01-28
7
259 Views
Last Modified: 2016-02-10
I have a query using linq to sql:

Dim SOs As IEnumerable(Of String) = (From i In dc.ShopOrders
                                             Order By i.SO_Number
                                             Select i.SO_Number).Distinct()

ShopOrders being a table in my database.

the order by clause does not seem to be working.

What am I doing wrong?

Also, I tried to do an IEnumerable.OrderBy after the fact, something like

SOs = SOs.orderby( ...

but couldn't get it to work.  How would I do that?


Thanks.
0
Comment
Question by:g_johnson
7 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Are you sure SO_Number is not string?
0
 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
what does your values look like?
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi g_johnson;

If I remember right from reading some Microsoft documentation somewhere the Distinct method does not guarantee to return the results in the same order. Try it like this

Dim SOs As IEnumerable(Of String) = (From i In dc.ShopOrders
                                     Select i.SO_Number).Distinct().OrderBy(Function(osn) osn)

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Author Comment

by:g_johnson
Comment Utility
Hello Fernando Soto -- Yes, I had run across that too.  The Disctinct operator is what was destroying it.  Your orderby worked, though I don't understand what it is doing.  Thanks for the help!
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Hi g_johnson;

This Linq query Iterate through all the ShopOrders and selects the single column SO_Number and returns it as a collection of String.
 
(From i In dc.ShopOrders
 Select i.SO_Number)

The Distinct method then Iterates through the collection of strings and removes all duplicates.

 .Distinct()

And finally once all duplicates have been removed we can sort the results. The OrderBy method here is taking a Lambda expression and ordering it by the only value comming in which is the string. All this is saying is that I have defined a Lambda expression and that function will be sending a single variable to the function which will be called osn but can be called anything you want. This variable will take on the input which is a collection of strings and pass it on to the function. In this case the function is not doing much except telling OrderBy to sort this value with the others that follow.

 .OrderBy(Function(osn) osn)

Hope that helps.
0
 
LVL 4

Author Comment

by:g_johnson
Comment Utility
Yeah -- I think I understand now.  I'm familiar with lambda expressions (somewhat) from c#.

Thanks for the clarification.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem, glad to help.
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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

762 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

7 Experts available now in Live!

Get 1:1 Help Now