There are two tables. It is necessary to choose from the second by a certain ID column, which occurs most often, and then from the first, by this ID, select the value of the name field.

I.e. From the second table, we take the most common ID from the second table, and drag NAME from the first one, where in the ID record it is equal to the pulled ID from the second table.

More details. The first table (Travels) consists of the fields in, name and other unimportant in this task. The second table (Group) consists of the id, name, and travel_id fields. The task. Find in the second table travel_id, which occurs most often and display the name of this object from the Travels table, that is, the record with id == travel_id.

  • like this: var arr = new int [] {1,2,3,3,2,3,2,3,2,5,1,2,2,2,2,2,4,3,5,1 , 2,4}; arr.GroupBy (el => el / * you will have el.Id or something like that * /). OrderBy (el => el.Count ()). Last (). Key // most common value is Specter
  • var result3 = entity.Travels.Where (tr => tr.id == (entity.Groups.GroupBy (el => el.travel_id) .OrderBy (el => el.Count ()). Last (). Key) ); Did so, did not work. - Jakeroid
  • and more? - Specter
  • Added more in question. The error is this. gyazo.com/5590f5b0b6a37192d0c276eb8e0cc52f - Jakeroid
  • try entity.Groups.GroupBy(el => el.travel_id).OrderByDescending(el => el.Count()).First().Key , should work :) - VladD

1 answer 1

введите код здесь Two working options.

 var mostFrequentID = entity.Groups .GroupBy(el => el.travel_id) .OrderByDescending(el => el.Count()) .First() .Key; var result3 = entity.Travels.Where(tr => tr.id == mostFrequentID); 

And.

 var result3 = entity.Travels.Where(tr => tr.id == (entity.Groups.GroupBy(el => el.travel_id).OrderByDescending(el => el.Count()).FirstOrDefault().Key));