Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

For Example:

My Row data is

C#
string[] filePaths1 = { "7.1", "7.1", "7.2", "7.2", "7.3", "7.4" };


I need a linq query that will give me the list of list with the number of unique data.

For example:


7.1 => 2
7.2 => 2
7.3 => 1
7.4 => 1

Thanks,
Posted

For example using Linq:
C#
string[] filePaths1 = { "7.1", "7.1", "7.2", "7.2", "7.3", "7.4" };

var qry = from s in filePaths1
		group s by s into grp
		select new{
			num = grp.Key,
			count = grp.Count()
		};
//then...
foreach(var o in qry)
{
    Console.WriteLine("{0} -> {1}", o.num, o.count);
}
 
Share this answer
 
v2
Comments
Peter Leow 8-Jan-15 4:46am    
5ed!
Maciej Los 8-Jan-15 4:46am    
Thank you, Peter ;)
Mayank Engineer 8-Jan-15 4:53am    
Thank you
Maciej Los 8-Jan-15 4:54am    
You're very welcome ;)
Mayank Engineer 8-Jan-15 5:00am    
How to access var qry count and it's key ?
Use a generic Dictionary<string, int>. Iterate over the original list, comparing every entry with the dictionary using the ContainsKey() method. Keys that don't exist yet, get a new entry in the dictionary with Add(whateverString, 1), existing keys just increase their value using YourDictionary[whateverString]++
 
Share this answer
 
You could do like this:

C#
string[] filePaths1 = { "7.1", "7.1", "7.2", "7.2", "7.3", "7.4" };

List<string> distinctFilePaths = filePaths1.Distinct().ToList();

foreach(string filePath in distinctFilePaths)
{
    int count = filePaths1.Count(x => x == filePath);
}
 
Share this answer
 
v2
Comments
TheRealSteveJudge 8-Jan-15 5:10am    
@downvoter: What is wrong with this solution?

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