GroupBy String and Count in LINQ?

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=(.*),. *$") . Groups1.

Value) . GroupBy(x => x) . Select(x=> new {Name = x.

Key, Count = x.Count()}) Query result for tested strings.

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=(.*),. *$") . Groups1.

Value) . GroupBy(x => x) . Select(x=> new {Name = x.

Key, Count = x.Count()}); Query result for tested strings.

Once you've extracted the string, just group by it and count the results: var query = from location in locations let name = ExtractNameFromLocation(location) group 1 by name in grouped select new { Name=grouped. Key, Count=grouped.Count() }; That's not particularly efficient, however. It has to do all the grouping before it does any counting.

Have a look at this VJ article for an extension method for LINQ to Objects, and this one about Push LINQ which a somewhat different way of looking at LINQ. EDIT: ExtractNameFromLocation would be the code taken from answers to your other question, e.g. Public static string ExtractNameFromLocation(string location) { var name = (from part in location. Split(',') let pair = part.

Split('=') where pair0.Trim() == "Name" select pair1.Trim()).FirstOrDefault(); return name; }.

Plz help with full query – KJai Feb 12 '09 at 6:38 ExtractNameFromLocation would do exactly what it sounds like - and others have provided code to do that in your other question: stackoverflow. Com/questions/539983/extract-portion-of-string – Jon Skeet Feb 12 '09 at 7:24.

Here is another LINQ alternative solution with a working example. Static void Main(string args) { System.Collections.Generic. List l = new List(); l.

Add("Theater=1, Name=regal, Area=Area"); l. Add("Theater=34, Name=Karm, Area=Area4445"); foreach (IGrouping g in l. GroupBy(r => extractName(r))) { Console.

WriteLine( string. Format("Name= {0} Count {1}", g. Key, g.Count()) ); } } private static string extractName(string dirty) { System.Text.

RegularExpressions. Match m = System.Text. RegularExpressions.Regex.

Match( dirty, @"(?Value : ""; }.

You're only iterating over the results. – Jon Skeet Feb 12 '09 at 7:28 Other than carelessness? No, i've updated my posting, thanks.

– James Feb 12 '09 at 7:33.

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. Which is the most short and precise way to do it?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions