LINQ将多行折叠为List

时间:2015-03-20 23:04:31

标签: c# linq entity-framework

我有以下对象来管理按群组发送电子邮件:

public class GroupEmailRecipients
{
     public string GroupName { get; set; }

     public List<EmailRecipient> EmailRecipients { get; set; }

     public GroupEmailRecipients()
     {
        EmailRecipients = new List<EmailRecipient>();
     }
}

public class EmailRecipient
{
    public string Email { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }
}

我有一个名为GroupContacts的表,其中包含:

GroupName | Email | FirstName | LastName
Name01 | john.smith@test.com | John | Smith
Name01 | jane.doe@test.com | Jane | Doe
Name02 | bill.smith@test.com | Bill | Smith

是否可以创建一个从GroupContacts表中填充List<GroupEmailRecipients>的LINQ语句?

List<GroupEmailRecipients>将填充为:

 First GroupEmailRecipients Object: 
 GroupName = "Name01"
 EmailRecipients = { Email= "john.smith@test.com", FirstName="John", 
 LastName="Smith"  } , { Email= "jane.doe@test.com",  FirstName="Jane",
 LastName = "Doe"  }

 Second GroupEmailRecipients Object: 
 GroupName = "Name02"
 EmailRecipients = { Email= "bill.smith@test.com", FirstName="Bill",
 LastName="Smith"  }

我正在使用EntityFramework 6.0,因此存根看起来像:

 List<GroupEmailRecipients> results = new List<GroupEmailRecipients>();

 using (var db = new MainContext()) 
{
     results = db.GroupContacts.[LINQ Syntax].ToList(); 
}

4 个答案:

答案 0 :(得分:1)

这应该有用。

db.GroupContacts.GroupBy(r => r.Name)
                .Select(group => new GroupEmailRecipients() 
                    { GroupName = group.Key, 
                      EmailRecipients = group.Select(x => new EmailRecipient()
                          { 
                            FirstName = x.FirstName, 
                            LastName = x.LastName, 
                            Email = x.Email 
                          })
                    });

答案 1 :(得分:1)

试试这个:

var groupEmails = db.GroupContacts
            .GroupBy(x => x.GroupName)
            .Select(x => new GroupEmailRecipients
            {
                GroupName = x.Key,
                EmailRecipients = x.Select(y => new EmailRecipient
                {
                    Email = y.Email,
                    FirstName = y.FirstName,
                    LastName = y.LastName
                }).ToList()
            }).ToList();

答案 2 :(得分:1)

[TestFixture]
public class GroupBY
{
    [Test]
    public void Test()
    {
        var list = new List<GroupContact>
        {
            new GroupContact
            {
                GroupName = "Name01",
                Email = "john.smith@test.com",
                FirstName = "John",
                LastName = "Smith"
            },
            new GroupContact
            {
                GroupName = "Name01",
                Email = "jane.doe@test.com",
                FirstName = "Jane",
                LastName = "Doe"
            },
            new GroupContact
            {
                GroupName = "Name02",
                Email = "bill.smith@test.com",
                FirstName = "Bill",
                LastName = "Smith"
            }
        };

        var res = list.GroupBy(g => g.GroupName).Select(g => new GroupEmailRecipients
        {
            GroupName = g.Key,
            EmailRecipients = g.Select(c => new EmailRecipient
            {
                Email = c.Email,
                FirstName = c.FirstName,
                LastName = c.LastName
            }).ToList()
        });

        foreach (var group in res)
        {
            Console.WriteLine("Group = " + group.GroupName);
            foreach (var recipient in group.EmailRecipients)
            {
                Console.WriteLine("\tRecipient - {0}, {1} - {2}", recipient.FirstName, recipient.LastName,recipient.Email);
            }

        }
    }
}

返回

Group = Name01
    Recipient - John, Smith - john.smith@test.com
    Recipient - Jane, Doe - jane.doe@test.com
Group = Name02
    Recipient - Bill, Smith - bill.smith@test.com

答案 3 :(得分:0)

的groupby超载
    groupContacts.GroupBy(gc=>gc.GroupName,(gn,gers)=> new GroupEmailRecipients() {GroupName=gn,EmailRecipients=gers.ToList()})