André Miranda
André Miranda

Reputation: 6588

Linq to count from inner lists

Let's suppose I have the following situation:

public class ABC
{

     public List<A> listA { get; set; }
     public List<B> listB { get; set; }
}

public class Test
{
    public void LetsCount(List<ABC> listABC)
    {
        //int total = it's the total of all listA plus the total of all listB inside listABC... is it possible via linq? 
    }
}

Is it possible to get this count via linq?

Thanks!!

Upvotes: 0

Views: 162

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

Use simple Enumerable.Sum (assume both listA and listB are not null, otherwise you need to add null-check):

int total = listABC.Sum(abc => abc.listA.Count + abc.listB.Count);

Upvotes: 6

Related Questions