Solved

Ordering IEnumberable in VB.Net

Posted on 2014-01-28
7
264 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
[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
7 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39816489
Are you sure SO_Number is not string?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39816646
what does your values look like?
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39816931
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
Independent Software Vendors: 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!

 
LVL 4

Author Comment

by:g_johnson
ID: 39817844
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 63

Expert Comment

by:Fernando Soto
ID: 39817911
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
ID: 39817934
Yeah -- I think I understand now.  I'm familiar with lambda expressions (somewhat) from c#.

Thanks for the clarification.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39817995
Not a problem, glad to help.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

691 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