2

I have list of households and each households includes a list of residents. I want to have a function that returns a list of all residents of all households.

What I have so far:

public List<Resident> Residents() {
    var list = new List<Resident>();
    Households.ForEach(x => list.AddRange(x.Residents));
    return list;
}

Is it possible to shorten this with a lambda? Something like this:

public List<Resident> Residents => { ??? }

I know if Households were a list of lists I could use SelectMany, however it is not.

EDIT1:

To make it clear, Household is a class, not a container.

EDIT2:

I tried the following

public List<Resident> Residents => Households.SelectMany(h => h.Residents);

However this gives me the error:

error CS1061: 'List< Household>' does not contain a definition for 'SelectMany' and no extension method 'SelectMany' accepting a first argument of type 'List< Household>' could be found (are you missing a using directive or an assembly reference?)

EDIT3:

Sorry, I thought I was clear. Household is a class which has a list of residents, like this:

class Household {
    public List<Resident> Residents { get; }
}
gartenriese
  • 4,131
  • 6
  • 36
  • 60
  • 6
    I don't understand your remark about SelectMany. Why can't you do `Households.SelectMany(x => x.Residents)`? – Kevin Gosse Mar 16 '16 at 10:34
  • 2
    What type is Hoseholds if not a list? Can't you do `.ToList()` on it before? Or have you forgot to add a `using` statement and that's why you don't see the `SelectMany`? – Niklas Mar 16 '16 at 10:35
  • 1
    As @KooKiz noted, from your code it looks like you can simply do SelectMany. – Cetin Basoz Mar 16 '16 at 10:35
  • This definitely *looks* like a list of lists and therefore SelectMany should work. See here: http://stackoverflow.com/questions/958949/difference-between-select-and-selectmany – sr28 Mar 16 '16 at 10:40
  • You start by saying: "I have list of households and each households has a list of residents". But then you say it is not a list of lists. `SelectMany` doesn't require lists anyway; it just requires `IEnumerable` – Dennis_E Mar 16 '16 at 10:41
  • Show the definition of this class as we can´t know how to transform an instance of it to a list or any other `IEnumerable`.. – MakePeaceGreatAgain Mar 16 '16 at 10:42
  • Your edit has made this totally unclear - You say `Households` is a "class, not a container" - what does that mean exactly? – Jamiec Mar 16 '16 at 10:43
  • This makes no sense. It sounds like you have 1 instance of 'HouseHold', which has a list of Residents. That will contain all your residents so nothing needs to be done. On the other hand it's more likely you have a LIST of 'HouseHold' and each of those list items contains a list of Residents. If that's the case its a list of lists scenario. – sr28 Mar 16 '16 at 10:47
  • 1
    Well err...no, it won't contain a definition for 'SeletMany', but it should have 1 for 'SelectMany'. – sr28 Mar 16 '16 at 10:48
  • 2
    You probably just forgot to add `using System.Linq;` – Dennis_E Mar 16 '16 at 10:48
  • 1
    `are you missing a using directive or an assembly reference` - well... are you? – Jamiec Mar 16 '16 at 10:51
  • @Dennis_E: That did it, thanks! – gartenriese Mar 16 '16 at 10:51

4 Answers4

4

You can do this in C# 6.0:

public List<Resident> Residents => Households.SelectMany(x => x.Residents).ToList(); 

You need to make sure that you have a using System.Linq; at the top of your code to get the IEnumerable<T> extension methods - which includes SelectMany.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
3

Your code can be shortened to

var residents = Households.SelectMany(x => x.Residents).ToList(); 

residents will then be a IEnumerable<Resident> ( or whatever type Residents is, I've assumed its a type called Resident).

You can tag .ToList() on the end if you actually need a List<Resident>.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
2

make sure that you are using System.Linq

using System.Linq;

Households.SelectMany(x => x.Residents) should work no matter if Residents is an array or a List<>

Menelaos Vergis
  • 3,715
  • 5
  • 30
  • 46
1

The following is the shortest I think:

public List<Resident> Residents() 
{
   return Households.SelectMany(x => x.Residents).ToList();
}
Mr. B
  • 2,845
  • 1
  • 21
  • 31