public class Emp
{
public int EmpId { get; set; }
public string Type { get; set; }
public List<string> Email { get; set; }
}
I fetch data from database and put it in a list
List<Emp> employees= new List<Emp>();
// fill the list here via DB call
The list would have, please also note that Type field would always be same for same user but email would be different
employees[0] = new Emp{ EmpId = 1, Type = "User", Email = "one@test.com" };
employees[1] = new Emp{ EmpId = 1, Type= "User", Email = "two@test.com" };
employees[2] = new Emp{ EmpId = 2, Type = "Test", Email = "three@test.com" };
employees[3] = new Emp{ EmpId = 2, Type= "Test", Email = "four@test.com"};
employees[4] = new Emp{ EmpId = 3, Type = "Test", Email = "five@test.com" };
employees[5] = new Emp{ EmpId = 3, Type= "Test", Email = "six@test.com"};
employees[6] = new Emp{ EmpId = 4, Type= "User", Email = "seven@test.com"};
I'm trying to group Emp based on their EmpId so the result should be a new list
Expected Result
Result = new Emp{ EmpId = 1, Type = "User", Email = "one@test.com", "two@test.com" };
new Emp{ EmpId = 2, Type = "Test", Email = "three@test.com", "four@test.com" };
new Emp{ EmpId = 3, Type = "Test", Email = "five@test.com", "six@test.com" };
new Emp{ EmpId = 4, Type = "User", Email = ""seven@test.com" };
//This is what I have done so far // Please let me know if this is incorrect
var result = from emp in employees
group emp.Email by new { emp.EmpId, emp.Type } into g
select new { Key = g.Key, Type = g.Key.Type, Emails = g.ToList() };
// My problem comes here when I loop this result
foreach (var r in result)
{
Console.WriteLine(r.Key.EmpId + "--" + r.Key.Type);
//This is where I need to get all emails of the Employee which I grouped
// iF I IMPLEMENT FOREACH
foreach (var e in r.Emails)
{
//?? WHAT i DO HERE
//e.?? to get email
}
// OR iF I IMPLEMENT FOR LOOP
for(int i = 0 ; i< r.Emails.Count; i++)
{
Console.WriteLine("Inner Loop" + "--" + r.Key.EmpId + "--" + r.Key.Type + "--" + r.Emails[0].ToString()); // r.Emails[0].ToString() prints out System.Collections.Generic.List '1[System.String]
}
}
Please let me know if I mad eany mistake or there's other way to do this. All I need is Group employees based on EmpID and also have their Type but grouped Emails.
Your group emp.Email by new { emp.EmpId, emp.Type }
means that each element of the group will have a key of the anonymous type, and an "element type" of List<string>
. You're then propagating that element type using Emails = g.ToList()
in your select
clause. Therefore I'd expect the type of r.Emails
to be List<List<string>>
(which you should be able to validate in Visual Studio by hovering over r.Emails
.
You could handle that in your loop - or you could just flatten it in your select
call, creating a new Emp
:
select new Emp {
EmpId = g.Key.EmpId,
Type = g.Key.Type,
Emails = g.SelectMany(x => x).ToList()
};
Here the SelectMany
call is just flattening the "sequence of lists" to a single sequence.
See more on this question at Stackoverflow