Linq – GroupBy String and Count in LINQ


I have got a collection. The coll has strings:

Location="Theater=1, Name=regal, Area=Area1"

Location="Theater=34, Name=Karm, Area=Area4445"

and so on. I have to extract just the Name bit from the string. For example, here I have to extract the text 'regal' and group the query. Then display the result as

Name=regal Count 33
Name=Karm  Count 22

I am struggling with the query:

Collection.Location.GroupBy(????);(what to add here)

Which is the most short and precise way to do it?

Best Solution

Yet another Linq + Regex approach:

string[] Location = {
                        "Theater=2, Name=regal, Area=Area1",
                        "Theater=2, Name=regal, Area=Area1",
                        "Theater=34, Name=Karm, Area=Area4445"

var test = Location.Select(
                            x => Regex.Match(x, "^.*Name=(.*),.*$")
                   .GroupBy(x => x)
                   .Select(x=> new {Name = x.Key, Count = x.Count()});

Query result for tested strings