Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

linq

I am some what new to LINQ so please cut me some slack.

I have a LINQ statement that I have grouped multiple columns on. Depending on the search it ranks every record with how much it matches the search.

  • 1 pt. for First Letter of First Name
  • 2 pts. for First 2 Letters of the First Name
  • 4 pts for First Name Matching
  • 8 pts. for Last Name Matching

So the First Order By is That. I then want a Thenby statement to order 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 LINQ statement works if you take out the thenby line. 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

enter image description here

OK I AM ADDING THIS HERE FOR THE COMMENTS BELOW BECAUSE I CAN'T ADD IMAGE TO COMMENT

enter image description here

enter image description here

like image 840
Mike Hewitt Avatar asked Jan 14 '23 08:01

Mike Hewitt


2 Answers

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

You can see an explanation in the ThenBy operator:

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.

like image 186
Yuriy Faktorovich Avatar answered Jan 16 '23 19:01

Yuriy Faktorovich


I don't believe you can use thenby in this fashion its not an Keyword but extension method. 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
like image 30
Bearcat9425 Avatar answered Jan 16 '23 17:01

Bearcat9425