The linq statement with multiple column groups, then not allowing

I'm kind of new to LINQ, so please cut me the slack.

I have a LINQ statement into which I have grouped several columns. Depending on the search, he evaluates each record as it matches the search.

  • 1 pt. for the first letter on behalf
  • 2 pts. for the first 2 letters of the name
  • 4 points to match the name
  • 8 points. for name matching

So the first order is this. Then I want the Thenby operator to be ordered by 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 the LINQ statement works, if you take out the thenby string. But once you put it on it, it won’t work.

That should work. Any help would be great

here is the mistake he shows me when I hung over him

enter image description here

OK I AM ADD THIS HERE FOR COMMENTS BELOW THE SUBSIDIARY THAT I CAN'T ADD AN IMAGE TO THE COMMENT

enter image description here

enter image description here

+4
source share
3 answers

thenby not a valid keyword, use orderby g.Max().ResultMatch descending, g.Key.FirstName ascending .

You can see the explanation in ThenBy :

In the syntax of a query expression, orderby [first criterion], [second criterion] (Visual C #) or order by [first criterion], [second criterion] (Visual Basic) translates into a ThenBy call.

+6
source

I do not believe that you can use this method in this way not for a keyword, but for extension. To do what you are looking for, you will need to do it.

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

When using query syntax, you separate multiple sort keys with commas, not thenby .

+1
source

Source: https://habr.com/ru/post/1481836/


All Articles