|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
About generic List<string>Hello!
I have a List collection with several identical strings. Is it any way to count how many of each identical string I have in the list without going through the list by using some iteration like foreach I just wonder if it's possible to use linq in a suitable way here. //Tony Yes, LINQ would be of use here but there will be a loop through the distinct
strings. You can use the Count extension method to get the count of each of them. var distinctStrings = list.Distinct(); // 'list' is assumed to be List<string> foreach (string eachString in distinctStrings) { int results = data.Count(s => s == eachString); // ... } Show quoteHide quote "Tony Johansson" <t.johans***@logica.com> wrote in message news:ecPX8saYJHA.2068@TK2MSFTNGP03.phx.gbl... > Hello! > > I have a List collection with several identical strings. Is it any way to > count how many of each identical string I have in the list without > going through the list by using some iteration like foreach > > I just wonder if it's possible to use linq in a suitable way here. > > //Tony > Tony Johansson wrote:
> Hello! Yes, you can get groups with each string value and count the strings in > > I have a List collection with several identical strings. Is it any way > to count how many of each identical string I have in the list without > going through the list by using some iteration like foreach > > I just wonder if it's possible to use linq in a suitable way here. > > //Tony > each group. Example: List<string> names = new List<string> { "John", "Peter", "Paul", "Anna", "Peter", "Anna", "Jane", "John", "Peter" }; var count = from string name in names group name by name into cnt select new { Name = cnt.Key, Count = cnt.Count() }; foreach (var c in count) { Console.WriteLine("{0} : {1}", c.Name, c.Count); } "Tony Johansson" <t.johans***@logica.com> wrote in message The standard way to find duplicates in a table using SQL is to do a news:ecPX8saYJHA.2068@TK2MSFTNGP03.phx.gbl... > I have a List collection with several identical strings. Is it any way to > count how many of each identical string I have in the list without > going through the list by using some iteration like foreach > > I just wonder if it's possible to use linq in a suitable way here. "select Count(*) as repeats, MyField Group By MyField Having repeats>1". There's no reason why you can't translate it into LINQ-to-objects if you wish to apply it to a List<string>. var duplicates = myList.GroupBy(x => x).Where(g => g.Count() > 1).Select(g => g.Key); Alberto Poblacion wrote:
Show quoteHide quote > "Tony Johansson" <t.johans***@logica.com> wrote in message Hm... That won't return the count for each string.> news:ecPX8saYJHA.2068@TK2MSFTNGP03.phx.gbl... >> I have a List collection with several identical strings. Is it any way >> to count how many of each identical string I have in the list without >> going through the list by using some iteration like foreach >> >> I just wonder if it's possible to use linq in a suitable way here. > > The standard way to find duplicates in a table using SQL is to do a > "select Count(*) as repeats, MyField Group By MyField Having repeats>1". > There's no reason why you can't translate it into LINQ-to-objects if you > wish to apply it to a List<string>. > > var duplicates = myList.GroupBy(x => x).Where(g => g.Count() > > 1).Select(g => g.Key); > It's not LINQ to Objects either... "Tony Johansson" <t.johans***@logica.com> wrote in message Either you can iterate or you can use a function that iterates for you, such news:ecPX8saYJHA.2068@TK2MSFTNGP03.phx.gbl... > Hello! > > I have a List collection with several identical strings. Is it any way to > count how many of each identical string I have in the list without > going through the list by using some iteration like foreach > > I just wonder if it's possible to use linq in a suitable way here. as the IEnumerable ForAll extension method. Either way an iteration will take place. List<string> strs; Dictionary<string, int> counts; foreach (string s in strs) { int tempCount; counts.TryGetValue(s, out tempCount); counts[s] = 1 + tempCount; } foreach (KeyValuePair<string, int> result in counts) { Debug.WriteLine(result.Key + " occurred " + result.Value + " time(s)."); }
Other interesting topics
Search all nodes using Linq
installer class failure Dynamically using object methods XML => Object stored in memory? xml Inheritance - Accessing two levels above mine Second message loop a Windows Forms application? Data Objects vs fields delegate parameter - can I do this? How to stop Invoked code running immediately after ShowDialog() closes |
|||||||||||||||||||||||