SelectMany Flattening a sequence of sequences
suggest changevar sequenceOfSequences = new [] { new [] { 1, 2, 3 }, new [] { 4, 5 }, new [] { 6 } };
var sequence = sequenceOfSequences.SelectMany(x => x);
// returns { 1, 2, 3, 4, 5, 6 }
Use SelectMany()
if you have, or you are creating a sequence of sequences, but you want the result as one long sequence.
In LINQ Query Syntax:
var sequence = from subSequence in sequenceOfSequences
from item in subSequence
select item;
If you have a collection of collections and would like to be able to work on data from parent and child collection at the same time, it is also possible with SelectMany
.
Let’s define simple classes
public class BlogPost
{
public int Id { get; set; }
public string Content { get; set; }
public List<Comment> Comments { get; set; }
}
public class Comment
{
public int Id { get; set; }
public string Content { get; set; }
}
Let’s assume we have following collection.
List<BlogPost> posts = new List<BlogPost>()
{
new BlogPost()
{
Id = 1,
Comments = new List<Comment>()
{
new Comment()
{
Id = 1,
Content = "It's really great!",
},
new Comment()
{
Id = 2,
Content = "Cool post!"
}
}
},
new BlogPost()
{
Id = 2,
Comments = new List<Comment>()
{
new Comment()
{
Id = 3,
Content = "I don't think you're right",
},
new Comment()
{
Id = 4,
Content = "This post is a complete nonsense"
}
}
}
};
Now we want to select comments Content
along with Id
of BlogPost
associated with this comment. In order to do so, we can use appropriate SelectMany
overload.
var commentsWithIds = posts.SelectMany(p => p.Comments, (post, comment) => new { PostId = post.Id, CommentContent = comment.Content });
Our commentsWithIds
looks like this
{
PostId = 1,
CommentContent = "It's really great!"
},
{
PostId = 1,
CommentContent = "Cool post!"
},
{
PostId = 2,
CommentContent = "I don't think you're right"
},
{
PostId = 2,
CommentContent = "This post is a complete nonsense"
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents