Let's dissect different parts of your Linq Query
. Here each call's result / output act as the Input for the next call to process the data
valueArray.GroupBy(value => value)
GroupBy
here has a same usage as in Sql, it leads to result in the format IEnumerable<IGrouping<Key,Value>>
, lambda =>
is a glue, where it provides each an every element in the collection valueArray
for processing. Only strange part is you take the whole object as a Key
, so the Result will be same object as key and value
in this case. In case, if it's a custom class / reference type and not primitive type
, then override Equals
and GetHashcode
methods for the correct operation, else it will do reference comparison of the objects. Your case key seems to be double - primitive type
, which is fine
OrderByDescending(value => value.Count())
This shall be simple, take the Count
of the IGrouping elements
, for each Grouping
and arrange in the descending
order. Result is IOrderedEnumerable<IGrouping<Key,Value>>
, which are arranged by the grouped elements Count
First()
Take First
element with maximum count value i.e Only one IGrouping<Key,Value>
is selected
Key
Take the Key of the First
element, and return as a result