You can use SelectMany and GroupBy as follows: -
var result = photos.SelectMany(x => x.Tags, (photosObj, tags) => new {photosObj, tags}) .GroupBy(x => x.tags) .Select(x => new { Tags = x.Key, PhotoId = String.Join(",",x.Select(z => z.photosObj.Id)) });
This will give you all the tags and their corresponding PhotoIds in comma separated format.
Update:
Sorry, you just need to count the number of Photos objects, in which case you just need to: -
var result = photos.SelectMany(x => x.Tags) .GroupBy(x => x) .Select(x => new { Tags = x.Key, PhotoCount = x.Count() });
So, suppose you have this data: -
List<Photo> photos = new List<Photo> { new Photo { Id =1, Caption = "C1", Tags = new List<string> { "T1", "T2", "T3" }}, new Photo { Id =2, Caption = "C2", Tags = new List<string> { "T4", "T2", "T5" }}, new Photo { Id =3, Caption = "C3", Tags = new List<string> { "T5", "T3", "T2" }} };
You will see below: -

Rahul singh
source share