?
Solved

Ordering IEnumberable in VB.Net

Posted on 2014-01-28
7
Medium Priority
?
265 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 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