Click here to Skip to main content
15,891,004 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have 2 columns returned from database as follow:
Name Country
-----------------
John  Australia
Jenny Australia
Bob   Japan


I want to count the number of person coming from the same country using C# but I could not quite achieve it. I am using for loop but the count will always give me 1 for each row.
My intended result should be look like:
Name Country     Count
----------------------
John  Australia    2
Jenny Australia    2
Bob   Japan        1



My current codes are as follow:
C#
foreach (TravellerModel travellerModel in travellerModelList.ModelList)
{
travellerModel.PersonCount = travellerModelList.ModelList.GroupBy(x => x.Country).Count();
}


But the end result would always as follow:
Name Country     Count
----------------------
John  Australia    1
Jenny Australia    1
Bob   Japan        1


Any help would be much appreciated. Thank you.

What I have tried:

1. Research online but could not find any as i am out of idea on which keyword should i use.
Posted
Updated 30-Mar-20 20:18pm
v2

Check this:
C#
void Main()
{
	List<TravellerModel> tms = new List<TravellerModel>()
	{
		new TravellerModel(){Name = "John", Country = "Australia"},
		new TravellerModel(){Name = "Jenny", Country = "Australia"},
		new TravellerModel(){Name = "Bob", Country = "Japan"}
	};
	
	//solution #1  
	var result = tms.GroupBy(x=>x.Country)
		.SelectMany(grp=> grp.Select(y=> new
		{
			Name = y.Name,
			Country = y.Country,
			Count = grp.Count()
		}))
		.ToList();
	foreach(var r in result)
	{
		Console.WriteLine($"{r.Name} {r.Country} {r.Count}");
	}


	//solution #2
	foreach(TravellerModel tm in tms)
	{
		int cnt  = tms.Where(x=>x.Country==tm.Country).Count();
		Console.WriteLine($"{tm.Name} {tm.Country} {cnt}");
	}

	
}

// Define other methods and classes here
class TravellerModel
{
	public string Name { get; set; }
	public string Country { get; set; }
}
 
Share this answer
 
The better way to do this is using database like

select Name,Country,count(*) No_of_times from table group by Name,Country


In C#, You need to do it using 2 Loops.
One for main table and another for distinct columns only.
C#
DataTable dt = new DataTable();
DataTable dt_old = new DataTable();
dt = dt_old.DefaultView.ToTable(true, "Name", "Country");
dt.Columns.Add("Count", typeof(int));
foreach (DataRow r in dt.Rows)
{
    DataTable dt_temp = new DataTable();
    dt_old.DefaultView.RowFilter = "Name='" + r["Name"] + "' AND Country='" + r["Country"] + "'";
    dt_temp = dt.DefaultView.ToTable();
    r["Count"] = dt_temp.Rows.Count;
}
Hope this will help you.
 
Share this answer
 
Comments
Maciej Los 31-Mar-20 2:22am    
Sorry, but the first part of your answer is wrong.
Note that Name is unique, so, you'll get count: {1, 1, 1}.

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