如何使用 VB.Net lambda 表达式按多列排序

发布于 2024-08-10 23:46:13 字数 642 浏览 3 评论 0原文

我对这个网站进行了简短的搜索,并用谷歌搜索了这个,但似乎找不到一个很好的例子。我仍在努力理解整个“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱我好吗 2024-08-17 23:46:13

我在 Google 快速搜索中找到了这篇 MSDN 文章
我猜你正在寻找的是这样的:

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

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
神回复 2024-08-17 23:46:13

您应该像这样使用 ThenBy

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文