如何使用VB.Net Lambda表达式按多列排序 [英] How to order by multiple columns using VB.Net lambda expressions
问题描述
我已经对该网站进行了简短搜索,并在Google上进行了搜索,但似乎找不到一个很好的例子.我仍在努力弄清整个"Lambda Expressions"问题.
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.
这里有人可以给我一个使用Lambda表达式使用VB.Net和Linq-to-SQL进行多列排序的示例吗?
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
注意:WebCategory对象具有一个子WebPage对象(基于外键).我想先通过WebPage.DisplayOrder进行订购,然后再通过WebCategory.DisplayOrder进行订购.
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
提前谢谢.
推荐答案
我发现 Google快速搜索中的这篇MSDN文章. 我猜你在找什么?
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
这篇关于如何使用VB.Net Lambda表达式按多列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!