1

I need to return a list within a list using linq(lambda), however, still not getting required syntax.

I have a list of WikiMeta which I want to query and return all the WikiGroups as a new list.

Note: Some properties have been removed from clarity.

List<WikiMeta> Meta;

public class WikiMeta
{
    public List<WikiGroup> Groups = new List<WikiGroup>();
}

public class WikiGroup
{
    public string Name { get; set; }
}
2
  • Question is unclear. Are you trying to filter a List<T> ? You can use Where Commented Oct 24, 2013 at 9:38
  • 2
    you might be looking for SelectMany, like: Meta.SelectMany(m => m.Groups); Commented Oct 24, 2013 at 9:38

1 Answer 1

5

You can get them all as a flat list using SelectMany:

var groups = meta.SelectMany(m => m.Groups).ToList();

This will give you a List<WikiGroup>.

Documentation for SelectMany.

You can perform any query you wish on the main List<WikiMeta> as such:

var filteredGroups = meta
    .Where(m => m.IsActive) // Fictional "IsActive" boolean property.
    .SelectMany(m => m.Groups)
    .ToList();

Using this fluent-style functional approach to querying collections, you can start to build up more complicated queries.

If you wish to leave the query deferred until it is iterated, remove ToList, which will give you an IEnumerable<WikiGroup>.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.