Linq Custom Sort Order

by Keith 17. December 2009 16:43

Today I was looking for a quick way to order some objects using linq to entities.  Now I don’t confess to be any great whizz with linq but searching online proved fruitless for what I had in mind.  Heres the lowdown…

Here is a sample collection that I have…

ID Name Type
1 Apple Fruit
2 Banana Fruit
3 Carrot Veg
4 Lamb Meat
5 Salmon Fish
6 Beef Meat
7 Pork Meat

 

I may filter this collection but I always want Fruit and Veg to appear at the end of the list.  As a SQL developer this was easy to achieve but in Linq I found little advice online. 

The solution was to create a small function that could be used to generate the additional IsFruitOrVeg info that I needed to sort by.

 

Public Function IsFruitOrVeg(x as String) as Integer

    Select Case x

        case "Fruit", "Veg" : return 1

        case else : return 0

    End Select

End Function

 

This could then be used in my linq query as part of the order by clause

 

Dim mylist = from f as foods _

    order by IsFruitOrVeg(f.type), f.type

 

This produces the desired effect of listing Fruit and Veg at the End of the list like this…

 

ID, Name, Type

5, “Salmon”, “Fish”

4, “Lamb”, “Meat”

6, “Beef, “Meat”

7, “Pork, “Meat”

1, “Apple”,”Fruit”

2, “Banana”, “Fruit”

3, “Carrot”,”Veg”

 

Now I would like to do this a bit neater but there is not much on lambda expressions in vb.net for this task, if you have any suggestions for how to do this then please leave a comment.  For everyone else hope this solution works for you too.

Tags:

Blog

Action Pack License Key – Hidden Feature

by keith 28. October 2009 14:47

This morning I wanted to install a fresh copy of Windows 7 Ultimate  from my Microsoft Action Pack Subscription.  And was confronted with the message β€œThe product key you typed cannot be used to activate windows on this computer.”  After speaking with a representative at Microsoft I discovered that this message is caused by the software being already activated on another system.  It turns out that the Ultimate license is for a single system whereas the Windows 7 Professional is for up to 10 systems.  So the long and short of it is that if you need to install windows on more than one system Windows 7 Professional is the one to for, and Ultimate is really just for a single install.  

Tags:

Xaml Goodness

by Administrator 16. September 2009 02:42

Hi All

I decided to setup this blog after much deliberation, to share ideas about XAML and the related subjects of WPF and Silverlight.  If you have a passsion for designing great apps using either of these technologies then hopefully this will be of use to you...  

My plan is to provide a series of short videos that will help cover a range of different design and technology advice that will be of interest to the developer community.

I hope that some of whats here will appeal to you.

Tags: , ,

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About me

My name is Keith Duncan, and I am a Independant Software Developer based in Aberdeenshire, Scotland.  I work for a diverse range of clients providing software solutions in a the following technologies:

  • .NET
  • VB.NET
  • SQL Server
  • ASP.NET
  • Javascript
  • AJAX
  • XML
  • Content Management Systems (CMS) : Joomla and DotNetNuke
  • MySQL
  • WPF
  • Silverlight

In addition, I also assist my local community by running a local school ICT club and support the local Scout group.

Tag cloud

Page List