How can I use LINQ to find the number of the largest group of objects in a collection of objects?
As an example:
struct MyObject { public int SomeInt; public int GroupInt; } List<MyObject> ObjectList = new List<MyOBject>(); // populate list int LargestGroup = ObjectList.GroupBy(x => x.GroupInt).Max().Count();
This does not work for me as it led to an error:
ArgumentException: at least one object must implement IComparable.
How can I fix this LINQ query (if it is incorrect) or what should I look for to prevent this error if it is correct?
" ", . ; , Enumerable.Max , Count.
Enumerable.Max
, , :
int largestGroupCount = ObjectList.GroupBy(x => x.GroupInt) .Max(g => g.Count());
Max, " Int32".
Max
:
int largestGroupCount = ObjectList.GroupBy(x => x.GroupInt) .Select(g => g.Count()) .Max();
, , .
Source: https://habr.com/ru/post/1786041/More articles:Bitwise and unsigned int operations in PHP - c ++How can you determine if a collection is limited? - phpSeasonal pricing for the face. - phpUse SimpleXML to parse multiple RSS feeds - phpComposite primary key update - database-designUnexpected T_STRING, waiting for T_OLD_FUNCTION or T_FUNCTION or T_VAR - phpUsing "multipart / form-data" - htmlProblem Using Paperclip and Ruby on Rails 3 - ruby | fooobar.comHow to get the real path to the application in the game - playframeworkGraphics on the command line Mathematica 7? - wolfram-mathematicaAll Articles