Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,
i have list of string filling like this
station1|debit
station1|volume
station2|pluit
station2|debit
station3|debit
station4|debit

and i want know count of station like
station1 ==>2 
station3 ==>1
Posted
Updated 14-Apr-15 0:50am
v3
Comments
Maciej Los 14-Apr-15 6:56am    
How exactly your list look like?

If i understand you correctly...

C#
List<string[]> sta = new List<string[]>(){
  new string[]{"station1", "debit"},
  new string[]{"station1", "volume"},
  new string[]{"station2", "pluit"},
  new string[]{"station2", "debit"},
  new string[]{"station3", "debit"},
  new string[]{"station4", "debit"}};

  var qry = sta.GroupBy(a=>a[0]).Select(grp=>new{station = grp.Key, count= grp.Count()});

  foreach(var r in qry)
  {
      Console.WriteLine("{0} - {1}", r.station, r.count);
  }


[EDIT]
In case of pipe "|" delimiter:

C#
List<string> sta = new List<string>(){"station1|debit",
"station1|volume",
"station2|pluit",
"station2|debit",
"station3|debit",
"station4|debit"};

var qry = sta.Select(a=>Tuple.Create(a.Split('|')[0], a.Split('|')[1]))
            .GroupBy(b=>b.Item1).Select(grp=>new{station=grp.Key, count = grp.Count()});
 
Share this answer
 
v2
Comments
Florian Braun 14-Apr-15 7:04am    
I like your solution
Maciej Los 14-Apr-15 7:13am    
Thank you ;)
Florian Braun 14-Apr-15 7:19am    
I changed your solution a little bit :)
Member 11573837 14-Apr-15 7:09am    
hi,
i don't have liste declared like this
List<string[]> sta = new List<string[]>();

i have
List<string> lst_Station = new List<string>();
and (station1|debit) it one string the pipe is juste deminateur
Florian Braun 14-Apr-15 7:21am    
arrg.. yours is obviously a lot more compact. i so need to learn linq
C#
public static void Main(string[] args)
        {
            List<string> sta1 = new List<string>();
            sta1.Add("station1|debit");
            sta1.Add("station1|volume");
            sta1.Add("station2|pluit");
            sta1.Add("station2|debit");
            sta1.Add("station3|debit");
            sta1.Add("station4|debit");

            List<string[]> sta = new List<string[]>();
            foreach (string s in sta1)
                sta.Add(s.Split('|'));


  var qry = sta.GroupBy(a=>a[0]).Select(grp=>new{station = grp.Key, count= grp.Count()});

  foreach(var r in qry)
  {
      Console.WriteLine("{0} - {1}", r.station, r.count);
  }
 
Share this answer
 
Comments
Maciej Los 14-Apr-15 7:21am    
5ed!
See updated answer (after EDIT). I use Tuple, so... there's no need to create another List<arrayofstring> ;)
Tomas Takac 14-Apr-15 7:21am    
+5, although it could be written as a one-liner ;-)
var output = input.Select(x => x.Split('|')).GroupBy(x => x[0]).Select(x => new { Name = x.Key, Count = x.Count()});
Maciej Los 14-Apr-15 7:22am    
Good catch, Tomas ;)
Florian Braun 14-Apr-15 7:23am    
thats a realy good one
Member 11573837 14-Apr-15 7:31am    
Big Thanks
You just need different data structure designed for fast search. For example, it could be
C#
class Station {
   internal uint Count { get; set; }
   internal string Name { get; set; } // "volume", etc
}
// then you could use 
var dictionary = new System.Collections.Generic.Dictionary<string, Station>;

Or you could use the structure encapsulating two dictionaries, one for finding "volume" and number by "station1", and another for funding "station1" by "volume" (I don't know what they mean, so I'm explaining it by example).

Dictionaries give you time complexity of O(1), that's why. So, it can be very beneficial for any considerable number of items. The use of reference types for all elements makes redundancy pretty low.

You can parse all your strings with '|' using string.Split just once, and then quickly work with your structures.

[EDIT]

You can add split string line by line and check is the key already exists. Use Dictionary.TryGetValue, because you also need to get the value of the key already exists. If it exist, just increment Count.

Dictionary.TryGetValue lookup will give you O(1) complexity.

[END EDIT]

Please see:
http://en.wikipedia.org/wiki/Hash_table,
http://en.wikipedia.org/wiki/Associative_array (this is how dictionaries work),
http://en.wikipedia.org/wiki/Time_complexity,
http://en.wikipedia.org/wiki/Big_O_notation.

—SA
 
Share this answer
 
v4
Comments
Maciej Los 14-Apr-15 12:35pm    
Sorry, Sergey, but i have one question: am i blind or you took a shortcut too much? I'm not sure how to use dictionary in this context.
Sergey Alexandrovich Kryukov 14-Apr-15 12:48pm    
Sorry, it looks like I misformatted the code, so I fixed it now.
Are you saying that something is not clear, need more explanation?
Well, in this sample, the key is a string like "station1", and the value is the structure. And the search is done by "station1", using Dictionary.TryGetValue.

More generally: I repeated many times, it's one of the worst present-day fallacies, to work with strings representing data, instead of data itself. This example shows that even string data should not be represented using just the class System.String.

—SA
Maciej Los 14-Apr-15 12:59pm    
The last statement is completely clear, but i think you need to explain it bit more. BTW: what data type is uing?
Sergey Alexandrovich Kryukov 14-Apr-15 13:09pm    
Typo. Thank you, I'll fix it and add more explanation.
—SA
Maciej Los 14-Apr-15 13:27pm    
I think you missed another one thing. Count couldn't be a Station class property/field. It always should be one, because each station have to be unique. Another class (which inherits from ICollectionBase - for example) should be a container for Station and Something what describes "debit", "pluit", "volume". This is how the skeleton have to look like ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900