如何使用 VB.Net lambda 表达式按多列排序
我对这个网站进行了简短的搜索,并用谷歌搜索了这个,但似乎找不到一个很好的例子。我仍在努力理解整个“Lambda 表达式”的事情。
这里有人可以给我一个使用 VB.Net 和 Linq-to-SQL 使用 lambda 表达式按多列排序的示例吗?
下面是我现有的代码,它返回一个使用单列对结果进行排序的有序列表:
Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder).ToList
注意:WebCategory 对象有一个子 WebPage 对象(基于外键)。我想先按 WebPage.DisplayOrder 排序,然后再按 WebCategory.DisplayOrder 排序。
我尝试链接顺序,如下所示,虽然它编译并运行,但它似乎没有按我想要的顺序返回数据。
Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder).OrderBy(Function(c As WebCategory) c.WebPage.DisplayOrder).ToList
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Google 快速搜索中找到了这篇 MSDN 文章。
我猜你正在寻找的是这样的:
I found this MSDN article in a quick Google search.
I guess what your looking for is this:
您应该像这样使用 ThenBy :
You should use ThenBy like this: