Link to home
Start Free TrialLog in
Avatar of g_johnson
g_johnsonFlag for United States of America

asked on

Ordering IEnumberable in VB.Net

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.
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Are you sure SO_Number is not string?
what does your values look like?
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of g_johnson

ASKER

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!
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.
Yeah -- I think I understand now.  I'm familiar with lambda expressions (somewhat) from c#.

Thanks for the clarification.
Not a problem, glad to help.