我已经对这个网站进行了简短的搜索,然后用Google搜索,但似乎无法找到一个好的例子。我仍然试图绕过整个“Lambda表达式”的事情。
这里的任何人都可以使用lambda表达式使用VB.Net和Linq-to-SQL为多个列排序示例吗?
这是我现有的代码,它使用单列返回有序列表以对结果进行排序:
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
提前致谢。
答案 0 :(得分:41)
我在Google快速搜索中找到了this MSDN article。 我想你想要的是这个:
Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder). _
ThenBy(Function(c As WebCategory) c.WebPage.DisplayOrder).ToList
答案 1 :(得分:13)
你应该像这样使用ThenBy:
Return _dbContext.WebCategories.OrderBy(Function(c As WebCategory) c.DisplayOrder) _
.ThenBy(Function(c As WebCategory) c.WebPage.DisplayOrder) _
.ToList()