I have the following class
public class Photo
{
public int Id { get; set; }
public string Caption { get; set; }
public Collection<string> Tags { get; set; }
}
A photo can have multiple Tags associated with it, and the same Tag can be used in any number of different Photos.
What I ultimately want to do is to retrieve a list of distinct tags, with a count of how many photos are assigned to each tag.
I have no control over the underlying data - I am using an API which has a Search method that I'm using to retrieve a collection of Photos, but there is no method to get a list of distinct tags.
So my question is, if I have a large collection of Photos, how can I use LINQ to get a distinct list of the tags contained within this list, and get the count (of photos) for each of those tags?
var tagCounts = photos
.SelectMany(photo => photo.Tags)
.GroupBy(tag => tag, (tag, group) => new { tag, count = group.Count() })
.ToDictionary(tuple => tuple.tag, tuple => tuple.count);
The reasoning is as follows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With