简体   繁体   English

一个linq语句,其中包含多个group by列,不允许使用thenby

[英]A linq statement with multiple group by columns not allowing a thenby

I am some what new to LINQ so please cut me some slack. 我是LINQ的新手,所以请给我一些懈怠。

I have a LINQ statement that I have grouped multiple columns on. 我有一个LINQ语句,我已经分组了多个列。 Depending on the search it ranks every record with how much it matches the search. 根据搜索,它会根据搜索的匹配程度对每条记录进行排名。

  • 1 pt. 1点 for First Letter of First Name 第一封名字
  • 2 pts. 2分 for First 2 Letters of the First Name 前2个字母的名字
  • 4 pts for First Name Matching 第一名匹配4分
  • 8 pts. 8分 for Last Name Matching 用于姓氏匹配

So the First Order By is That. 所以第一顺序就是那样。 I then want a Thenby statement to order by FirstName 然后我想要一个Thenby语句按FirstName排序

var ResultsListOrdered = from O in ResultsList
                                         group O by new
                                         {
                                             O.FirstName,
                                             O.LastName,
                                             O.SSN,
                                             O.Email,
                                             O.Phone
                                         } into g
                                         orderby g.Max().ResultMatch descending
                                         thenby g.Key.FirstName ascending
                                         select new SearchResultViewModel
                                         {
                                             ID = g.Max().ID,
                                             FirstName = ti.ToTitleCase(g.Key.FirstName.ToLower()),
                                             LastName = ti.ToTitleCase(g.Key.LastName.ToLower()),
                                             SSN = g.Key.SSN,
                                             Email = g.Key.Email.ToLower(),
                                             Phone = g.Key.Phone,
                                             ResultMatch = g.Max().ResultMatch
                                         };`

if LINQ statement works if you take out the thenby line. 如果取出thenby行,如果LINQ语句有效。 But as soon as you put it in it does not work. 但是只要你把它放进去就行不通。

This should work. 这应该工作。 Any help would be great 任何帮助都会很棒

here is the error that it shows me when I hover over it 这是我将鼠标悬停在它上面时显示的错误

在此输入图像描述

OK I AM ADDING THIS HERE FOR THE COMMENTS BELOW BECAUSE I CAN'T ADD IMAGE TO COMMENT 好的,我在这里添加以下评论,因为我不能添加图片给评论

在此输入图像描述

在此输入图像描述

thenby isn't a valid keyword, use orderby g.Max().ResultMatch descending, g.Key.FirstName ascending . thenby不是有效的关键字,使用orderby g.Max().ResultMatch descending, g.Key.FirstName ascending

You can see an explanation in the ThenBy operator: 您可以在ThenBy运算符中看到解释:

In query expression syntax, an orderby [first criterion], [second criterion] (Visual C#) or Order By [first criterion], [second criterion] (Visual Basic) clause translates to an invocation of ThenBy. 在查询表达式语法中,orderby [第一标准],[第二标准](Visual C#)或Order By [第一标准],[第二标准](Visual Basic)子句转换为ThenBy的调用。

I don't believe you can use thenby in this fashion its not an Keyword but extension method. 我不相信你能以这种方式使用thenby它不是关键字而是扩展方法。 In order to do what you are looking to do you would need to do this. 为了做你想做的事,你需要这样做。

orderby g.Max().ResultMatch descending, g.Key.FirstName ascending

当使用查询语法,你用逗号分隔您的多个排序键,而不是thenby

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM