Say you have the need to flatten a collection of albums and retrieve all the photos within the albums and put them into a single album, you can use the SelectMany() notation in linq.
1: using System.Collections.Generic;
2: using System.IO;
3:
4: using System.Linq;
5:
6: class Program
7: {
8: static void Main(string[] args)
9: {
10: var data = GetAlbums();
11: var flattenAlbum = new Album { Id = 0, Name = "Flattenedalbum",
12: Photos = data.SelectMany(p => p.Photos).ToList() };
13: }
14:
15: private static IEnumerable<Album> GetAlbums()
16: {
17: var data = new List<Album>
18: {
19: new Album
20: {
21: Id = 1, Name = "Album1", Photos = new List<Photo>
22: {
23: new Photo
24: {
25: Id = 1, Name = "Photo1"
26: },
27: new Photo
28: {
29: Id = 2, Name = "Photo2"
30: },
31: new Photo
32: {
33: Id = 3, Name = "Photo3"
34: }
35: }
36: },
37: new Album
38: {
39: Id = 2, Name = "Album2", Photos = new List<Photo>
40: {
41: new Photo
42: {
43: Id = 4, Name = "Photo4"
44: },
45: new Photo
46: {
47: Id = 5, Name = "Photo5"
48: },
49: new Photo
50: {
51: Id = 6, Name = "Photo6"
52: },
53: new Photo
54: {
55: Id = 7, Name = "Photo7"
56: }
57: }
58: }
59: };
60: return data;
61: }
62: }
63:
64: class Album
65: {
66: public int Id { get; set; }
67: public string Name { get; set; }
68: public List<Photo> Photos { get; set; }
69: }
70:
71: class Photo
72: {
73: public int Id { get; set; }
74: public string Name { get; set; }
75: public Stream Data { get; set; }
76: }