How to order by multiple columns using VB.Net lambda expressions

33,873

Solution 1

I found this MSDN article in a quick Google search. I guess what your looking for is this:

Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder). _
ThenBy(Function(c As WebCategory) c.WebPage.DisplayOrder).ToList

Solution 2

You should use ThenBy like this:

Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder) _
                               .ThenBy(Function(c As WebCategory) c.WebPage.DisplayOrder) _
                               .ToList()
Share:
33,873
camainc
Author by

camainc

My day job: I am a senior-level .Net Developer, a mentor, and a part-time programming instructor. My passion: I'm the Co-Founder and Director of Myanmar Hope Christian Mission, Inc., an international non-profit dedicated to helping the tribal people of northwest Myanmar rise out of poverty. My hobbies: Watercolor painting and reading.

Updated on July 09, 2022

Comments

  • camainc
    camainc almost 2 years

    I've done a brief search of this site, and googled this, but can't seem to find a good example. I'm still trying to get my head around the whole "Lambda Expressions" thing.

    Can anyone here give me an example ordering by multiple columns using VB.Net and Linq-to-SQL using a lambda expression?

    Here is my existing code, which returns an ordered list using a single-column to order the results:

    Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder).ToList
    

    Note: The WebCategory object has a child WebPage object (based on a foreign key). I'd like to order by WebPage.DisplayOrder first, then by WebCategory.DisplayOrder.

    I tried chaining the order bys, like below, and though it compiled and ran, it didn't seem to return the data in the order I wanted.

    Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder).OrderBy(Function(c As WebCategory) c.WebPage.DisplayOrder).ToList
    

    Thanks in advance.

  • camainc
    camainc over 14 years
    Thanks, that's just what I was looking for. What were the Google search terms that you used to find that article, if I might ask?
  • camainc
    camainc over 14 years
    Thanks! You answered this at the same time as HuBeZa, but since you already have more badges than he does, I gave the answer to him.
  • evilspoons
    evilspoons almost 12 years
    Wow, I was doing (what I thought was) exactly this and it wouldn't work, I was getting some sort of Linq-related exception... getting incredibly frustrated... then I noticed I forgot the last ".ToList" part. I know it's obvious in retrospect that it needs to be the right data type, but I guess I was having one of those "forest for the trees" moments. Thanks for the information!
  • PeterCo
    PeterCo over 7 years
    BTW: If you need your own IComparer, you have to use it twice (once for the .OrderBy and once for the .ThenBy command). Just add , New MyOwnIComparer and of course your code for your class with the name MyOwnIComparer