Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ distinct with corresponding counts based on a List property

Tags:

c#

.net

linq

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?

like image 644
marcusstarnes Avatar asked Apr 30 '15 19:04

marcusstarnes


1 Answers

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.

  • Obtain the sequence of all tags, with repetition if multiple photos have the same tag.
  • Group the repeated tags by their value, and count the number of tags in each group.
  • Construct a dictionary mapping from each tag to the number of photos that have that tag.
like image 78
Timothy Shields Avatar answered Nov 15 '22 16:11

Timothy Shields