23

Using LINQ how to select from a List within a List

public class Model
{
    public string application { get; set; }

    public List<Users> users { get; set; }
}

public class Users
{
    public string name { get; set; }

    public string surname { get; set; }
}

List<Model> list = new List<Model>();

I need to select list where application = "applicationame" and users where surname = "surname" into one list.

1
  • 1
    What is the output you want? A list of Model? A list of User? Commented Feb 2, 2013 at 16:40

4 Answers 4

39

If you want to filter the models by applicationname and the remaining models by surname:

List<Model> newList = list.Where(m => m.application == "applicationname")
    .Select(m => new Model { 
        application = m.application, 
        users = m.users.Where(u => u.surname == "surname").ToList() 
    }).ToList();

As you can see, it needs to create new models and user-lists, hence it is not the most efficient way.

If you instead don't want to filter the list of users but filter the models by users with at least one user with a given username, use Any:

List<Model> newList = list
    .Where(m => m.application == "applicationname"
            &&  m.users.Any(u => u.surname == "surname"))
    .ToList();
Sign up to request clarification or add additional context in comments.

Comments

12

You have to use the SelectMany extension method or its equivalent syntax in pure LINQ.

(from model in list
 where model.application == "applicationname"
 from user in model.users
 where user.surname == "surname"
 select new { user, model }).ToList();

1 Comment

Like your use of anonymous classes.
3
list.Where(m => m.application == "applicationName" && 
           m.users.Any(u => u.surname=="surname"));

if you want to filter users as TimSchmelter commented, you can use

list.Where(m => m.application == "applicationName")
    .Select(m => new Model
    {
        application = m.application,
        users = m.users.Where(u => u.surname=="surname").ToList()
    });

3 Comments

He needs to select users where surname = "surname", not models which contain at least one user with surname="surname". Hence i think he wants to filter the users in each filtered model.
@TimSchmelter The kewword is: i think he wants ....
Yes, i'm not sure, but that's what OP has said. Otherwise he should have said that he wants models with application = "applicationame" which contains a user with surname = "surname".
-1

After my previous answer disaster, I'm going to try something else.

List<Model> usrList  = 
(list.Where(n => n.application == "applicationame").ToList());
usrList.ForEach(n => n.users.RemoveAll(n => n.surname != "surname"));

2 Comments

Inside your foreach "n" is referencing to the objects in the main data source (on memory), so you are modifying objects in the source list!
Plus - ForEach returns void which you assume it returns IEnumerable<Model>.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.