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

I have one string variable which contains
120, 130, 134, 138, 151, 3810


i just want to get distinct id count .

Please suggest
Posted

Try this:

C#
string s = "120, 130, 134, 138, 151, 3810";
 
var qry = s.Split(',')
        .Select(a=>Convert.ToInt32(a.Trim()))
		.GroupBy(a=>a)
		.Select(grp=>new
			{
				value = grp.Key,
				count = grp.Count()
			});


Result:
value count
120   1
130   1
134   1
138   1
151   1
3810  1


[EDIT]

C#
var qry = s.Split(',')
        .Select(a=>Convert.ToInt32(a.Trim()))
        .Distinct()
        .Count();
//returs: 6


[EDIT2]
According to OP comments:

C#
List<string> s = new List<string>()
        {
            "120, 130, 134, 138, 151, 3810",
            null,
            "120, 130, 134, 202, 1151, 3810"
        };

var qry = s.SelectMany(a=>(a ?? string.Empty).Split(new char[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries))
        .Distinct()
        .Count();
//returns: 8
 
Share this answer
 
v4
Comments
Torakami 14-May-15 2:41am    
I want total count of how many ids are available , but the total count should not add duplicate records
Maciej Los 14-May-15 2:44am    
See updated answer.
Torakami 14-May-15 3:31am    
thanks . but there is slight issue i am facing , how to get disctinct if multiple items gets added


objObjectives = new ActusLibrary.Objectives();
{
objObjectives.DepartmentName = item.DepartmentName;
objObjectives.TotalLive = item.Live_Total;
objObjectives.TotalDraft = item.Draft_Total;
objObjectives.TotalCompleted = item.Completed_Total;
objObjectives.TotalEmployees = item.Live_EmpIDs.Split(',').Select(a => string.IsNullOrEmpty(a) ? 0 : Convert.ToInt32(a.Trim())).Distinct().Count() +
item.Draft_EmpIDs.Split(',').Select(a => string.IsNullOrEmpty(a) ? 0 : Convert.ToInt32(a.Trim())).Distinct().Count() +
item.Completed_EmpIDs.Split(',').Select(a => string.IsNullOrEmpty(a) ? 0 : Convert.ToInt32(a.Trim())).Distinct().Count();
}
live draft and completed merging should be distinct employee count
Maciej Los 14-May-15 3:40am    
Sorry, but i do not see your data and can't read direct from your screen ;(
You have to provide more information if you want my help. On this piece of code i can only guess, that you have extra values, becasue of null replacement with zero: string.IsNullOrEmpty(a) ? 0 . Try: item.Live_EmpIDs.Split(',').Distinct().Count() + item.Draft_EmpIDs.Split(',').Distinct().Count() + item.Completed_EmpIDs.Split(',').Distinct().Count();
or
item.ResultSet.Split(',').Where(a=>a ?? 0 != 0).Distinct().Count() where ResultSet is one of xxx_EmpIDs collection.
Maciej Los 14-May-15 4:38am    
See updated answer.
Convert the string into an array or List<string> using string.Split(variable,',')
Make sure you trim the spaces.
You can then use Linq to select distinct.
 
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