分组Linq问题,无法做到正确

时间:2014-10-16 10:35:18

标签: c# linq

public class Emp
{
     public int EmpId { get; set; }
     public string Type { get; set; }
     public List<string> Email { get; set; }             
}

我从数据库中获取数据并将其放入列表中

List<Emp> employees= new List<Emp>();
// fill the list here via DB call

列表会有,请注意同一用户的类型字段总是相同但电子邮件会有所不同

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"}; 

我正在尝试根据他们的EmpId对Emp进行分组 所以结果应该是一个新列表

预期结果

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" };

//这是我到目前为止所做的 //如果这不正确,请告诉我

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() };

//当我循环这个结果时,我的问题就出现了

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]
    }
} 

如果我犯了很多错误或有其他方法可以解决这个问题,请告诉我。 我需要的只是基于EmpID的团队员工,还有他们的类型但分组的电子邮件。

5 个答案:

答案 0 :(得分:1)

您的group emp.Email by new { emp.EmpId, emp.Type }表示该组的每个元素都有一个匿名类型的键,以及一个&#34;元素类型&#34; List<string>。然后,您在Emails = g.ToList()子句中使用select传播该元素类型。因此,我希望r.Emails的类型为List<List<string>>(您应该能够通过将鼠标悬停在r.Emails上来在Visual Studio中进行验证。

可以在你的循环中处理它 - 或者你可以在select电话中展平它,创建一个新的Emp

select new Emp {
    EmpId = g.Key.EmpId,
    Type = g.Key.Type,
    Emails = g.SelectMany(x => x).ToList()
};

此处SelectMany调用只是扁平化列表序列&#34;一个序列。

答案 1 :(得分:0)

也许稍微改变可能会有用:如果您将电子邮件属性从列表更改为字符串,您可以将员工分组如下:

// Group by EmpId
var group = employees.GroupBy(e => e.EmpId);

并获取单个emp的列表,如下所示:

// Example get email List of first emp
group.First().Select(g => g.Email);

答案 2 :(得分:0)

您可以更改Emp课程,以便EMailstring而不是List<string>

然后,foreach循环变为

foreach(string e in r.EMails){
//e holds the EMail
//do stuff
}

答案 3 :(得分:0)

试试这个: -

var query1 = from emp in employees
                             group emp.Email by new { emp.EmpId, emp.Type } into empgroup
                             select new
                             {
                                 UserId = empgroup.Key.EmpId,
                                 EmployeeType = empgroup.Key.Type,
                                 EmaiIds = empgroup.SelectMany(x => x)
                             };


                foreach (var x in query1)
                {
                    Console.WriteLine(x.UserId);
                    Console.WriteLine(x.EmployeeType);
                    foreach (var emails in x.EmaiIds)
                    {
                        Console.WriteLine(emails);
                    }
                }

答案 4 :(得分:0)

void Main()
{
    List<Emp> employees= new List<Emp>();
    employees.Add(new Emp{ EmpId = 1, Type = "User", Email = "one@test.com" }); 
    employees.Add(new Emp{ EmpId = 1, Type = "User", Email = "two@test.com" }); 
    employees.Add(new Emp{ EmpId = 2, Type = "Test", Email = "three@test.com" });  
    employees.Add(new Emp{ EmpId = 2, Type = "Test", Email = "four@test.com" }); 
    employees.Add(new Emp{ EmpId = 3, Type = "Test", Email = "five@test.com" }); 
    employees.Add(new Emp{ EmpId = 3, Type = "Test", Email = "six@test.com" }); 
    employees.Add(new Emp{ EmpId = 4, Type = "User", Email = "seven@test.com" });  

    var groupedList = 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() };

    foreach (var result in groupedList)
    {
        //I'm using LINQPad to output the results
        result.Key.EmpId.Dump();
        foreach(var email in result.Emails)
        {
        email.Dump();
        }   
    }

}
public class Emp
{
     public int EmpId { get; set; }
     public string Type { get; set; }
     public string Email { get; set; }             
}

我的结果是:

enter image description here

...或

void Main()
{
    List<Emp> employees= new List<Emp>();
    employees.Add(new Emp{ EmpId = 1, Type = "User", Emails = new List<string>(){"one@test.com"} }); 
    employees.Add(new Emp{ EmpId = 1, Type = "User", Emails = new List<string>(){"two@test.com"} }); 
    employees.Add(new Emp{ EmpId = 2, Type = "Test",  Emails = new List<string>(){"three@test.com"}  });  
    employees.Add(new Emp{ EmpId = 2, Type = "Test",  Emails = new List<string>(){"four@test.com"} }); 
    employees.Add(new Emp{ EmpId = 3, Type = "Test",  Emails = new List<string>(){"five@test.com"}  }); 
    employees.Add(new Emp{ EmpId = 3, Type = "Test",  Emails = new List<string>(){"six@test.com"} }); 
    employees.Add(new Emp{ EmpId = 4, Type = "User", Emails = new List<string>(){"seven@test.com"} });  

        var groupedList = from emp in employees
                    group emp.Emails by new { emp.EmpId, emp.Type } into g
                    select new Emp {
                                        EmpId = g.Key.EmpId,
                                        Type = g.Key.Type,
                                        Emails = g.SelectMany(x => x).ToList()
                                    };
        foreach (var result in groupedList)
        {  //I'm using LINQPad to output    
        result.EmpId.Dump();
        result.Emails.ForEach(e => e.Dump());               
        }

}
public class Emp
{
     public int EmpId { get; set; }
     public string Type { get; set; }
     public List<string> Emails { get; set; }             
}

我的结果也是:

enter image description here