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; }
}
12
  • 6
    I don't understand your remark about SelectMany. Why can't you do Households.SelectMany(x => x.Residents)? Commented Mar 16, 2016 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? Commented Mar 16, 2016 at 10:35
  • 1
    As @KooKiz noted, from your code it looks like you can simply do SelectMany. Commented Mar 16, 2016 at 10:35
  • 1
    Well err...no, it won't contain a definition for 'SeletMany', but it should have 1 for 'SelectMany'. Commented Mar 16, 2016 at 10:48
  • 2
    You probably just forgot to add using System.Linq; Commented Mar 16, 2016 at 10:48

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the only answer that mentioned using System.Linq in their answer, so I'll accept this one.
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>.

Comments

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<>

Comments

1

The following is the shortest I think:

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

2 Comments

You dont need the AddRange, and actually that wont compile as list does not exist in your code.
Yep just realized that I didn't pull that out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.