Click here to Skip to main content
15,867,328 members
Please Sign up or sign in to vote.
4.50/5 (3 votes)
See more:
Hi,
I have a List of string type and I have added string as

'abc','abc','xyz'.


I want to count the distinct values of this list .

Output should be 2 for above.

thanks
mohd wasif
Posted

Hi ,
Another way
C#
List<string> lst = new List<string> { "abc", "abc", "xyz" };
       int res = (from x in lst
                  select x).Distinct().Count();

Best Regards
M.Mitwalli
 
Share this answer
 
Comments
Ganesh Nikam 11-Sep-12 6:01am    
5+
Mohamed Mitwalli 11-Sep-12 6:03am    
Thanks Ganesh
Ganesh Nikam 11-Sep-12 6:43am    
always welcome
Member 11925473 6-Jan-16 8:52am    
Years later and it helped me too thank you
Mohamed Mitwalli 7-Jan-16 5:21am    
your welcome any time :)

It can be something like:


C#
List<string> myList = new List<string> {"abc", "abc", "xyz"};
int distinctCount = myList.Distinct().Count();
 
Share this answer
 
Comments
Mohamed Mitwalli 11-Sep-12 4:34am    
5+
Shmuel Zang 11-Sep-12 6:10am    
Thank you Mohamed.
Ganesh Nikam 11-Sep-12 6:00am    
5+
Shmuel Zang 11-Sep-12 6:10am    
Thank you Ganesh.
Ganesh Nikam 11-Sep-12 6:43am    
always welcome
Hello

you will get distinct record using LINQ Query


C#
 // my arry sting 
 string[] filePaths = {"abc","abc","xyz"};

filePaths = filePaths.Cast<string>().Distinct().ToArray(); 

</string>


let me know if any query

Thanks
Gaurav Dhol
 
Share this answer
 

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