Matthias
Matthias

Reputation: 280

Get count of longest list inside a list with LINQ

I have the following structure:

class Attribute
{
   List<Value> values;
}    

List<Attribute> attributes;

I need to get the Count of the longest List<Value> from the list of attributes. Is there a simple solution using Linq?

I already tried two nested loops, which is not very performant.

Upvotes: 0

Views: 1753

Answers (2)

Adil
Adil

Reputation: 148110

You can use Enumerable.Max to get the list with Maximum size using the Count attribute.

var max = attributes.Max(lst=>lst.values.Count);

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

var max = attributes.Max(a => a.values.Count)

Upvotes: 3

Related Questions