Solved

Ordering IEnumberable in VB.Net

Posted on 2014-01-28
7
261 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
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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

778 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