Home All Groups Group Topic Archive Search About

About generic List<string>

Author
19 Dec 2008 7:27 AM
Tony Johansson
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

Author
19 Dec 2008 8:37 AM
Stanimir Stoyanov
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);

                // ...
            }
--
Stanimir Stoyanov
http://stoyanoff.info

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
>
Are all your drivers up to date? click for free checkup

Author
19 Dec 2008 8:49 AM
Göran_Andersson
Tony Johansson wrote:
> 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, you can get groups with each string value and count the strings in
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);
}

--
Göran Andersson
_____
http://www.guffa.com
Author
19 Dec 2008 8:50 AM
Alberto Poblacion
"Tony Johansson" <t.johans***@logica.com> wrote in message
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);
Author
19 Dec 2008 12:43 PM
Göran_Andersson
Alberto Poblacion wrote:
Show quoteHide quote
> "Tony Johansson" <t.johans***@logica.com> wrote in message
> 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);
>

Hm... That won't return the count for each string.

It's not LINQ to Objects either...

--
Göran Andersson
_____
http://www.guffa.com
Author
19 Dec 2008 7:53 PM
Ben Voigt [C++ MVP]
"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.

Either you can iterate or you can use a function that iterates for you, such
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).");
}

Bookmark and Share