如何合并两个列表 [英] How to Merge two lists

查看:97
本文介绍了如何合并两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想合并以下课程的两个列表

I want to merge two list of the following class

public class ContactsByUser
{
    public int UserId { get; set; }
    public string Username { get; set; }
    public int CountContacts { get; set; }
    public int CountBehaviorInterview { get; set; }

    public override bool Equals(object obj)
    {
        return this.UserId == ((ContactsByUser)obj).UserId;
    }

    public override int GetHashCode()
    {
        return UserId.GetHashCode();
    }
}

现在我有2个包含以下内容的列表

Now I have 2 lists containing the following

    List<ContactsByUser> contacts = new List<ContactsByUser>();
contacts.Add( new ContactsByUser { UserId = 1, Username = user1, CountContacts = 42, CountBehaviorInterview = 0});
contacts.Add( new ContactsByUser { UserId = 2, Username = user2, CountContacts = 25, CountBehaviorInterview = 0});
contacts.Add( new ContactsByUser { UserId = 3, Username = user3, CountContacts = 7, CountBehaviorInterview = 0});
contacts.Add( new ContactsByUser { UserId = 4, Username = user4, CountContacts = 10, CountBehaviorInterview = 0});
contacts.Add( new ContactsByUser { UserId = 5, Username = user5, CountContacts = 23, CountBehaviorInterview = 0});
contacts.Add( new ContactsByUser { UserId = 6, Username = user6, CountContacts = 60, CountBehaviorInterview = 0});
contacts.Add( new ContactsByUser { UserId = 7, Username = user7, CountContacts = 3, CountBehaviorInterview = 0});

List<ContactsByUser> interviews = new List<ContactsByUser>();
interviews .Add( new ContactsByUser { UserId = 1, Username = user1, CountContacts = 0, CountBehaviorInterview = 33});
interviews .Add( new ContactsByUser { UserId = 2, Username = user2, CountContacts = 0, CountBehaviorInterview = 21});
interviews .Add( new ContactsByUser { UserId = 3, Username = user3, CountContacts = 0, CountBehaviorInterview = 8});
interviews .Add( new ContactsByUser { UserId = 4, Username = user4, CountContacts = 0, CountBehaviorInterview = 17});

合并这两个列表后,我希望得到的结果如下

What I would like to have when I merge these 2 lists is the following result

{ UserId = 1, Username = user1, CountContacts = 42, CountBehaviorInterview = 33});
{ UserId = 2, Username = user2, CountContacts = 25, CountBehaviorInterview = 21});
{ UserId = 3, Username = user3, CountContacts = 7, CountBehaviorInterview = 8});
{ UserId = 4, Username = user4, CountContacts = 10, CountBehaviorInterview = 17});
{ UserId = 5, Username = user5, CountContacts = 23, CountBehaviorInterview = 0});
{ UserId = 6, Username = user6, CountContacts = 60, CountBehaviorInterview = 0});
{ UserId = 7, Username = user7, CountContacts = 3, CountBehaviorInterview = 0});

在第一个列表上,CountBehaviorInterview属性将始终为0,在第二个列表上,CountContacts将始终为0.

On the first list, the CountBehaviorInterview property will always be 0, and on the second list the CountContacts will always be 0.

我尝试使用union,但它所做的只是返回一个列表,其中包含2个列表.有没有一种方法可以按照我的需要进行合并?

I have tried using union but all it does is return a list with the 2 lists inside. Is there a way merge these the way i need?

推荐答案

如果可以更改contacts列表中的对象,可以在foreach中使用简单的Join:

If you're fine with changing the objects in the contacts list, you could use a simple Join with foreach:

foreach(var joined in contacts.Join(interviews, 
                                    o => o.UserId, 
                                    i => i.UserId, 
                                    (originalContact, i) => new {originalContact, i.CountBehaviorInterview}))
{
    joined.originalContact.CountBehaviorInterview = joined.CountBehaviorInterview;
}


如果要创建ContactsByUser的新实例,那么您正在寻找左外部联接,如下所示:


If you want to create new instances of ContactsByUser, then you're looking for a left outer join, something like this:

var result = from i in contacts
             join o in interviews on i.UserId equals o.UserId into j
             from o in j.DefaultIfEmpty()
             select  new ContactsByUser
                     {
                         UserId = i.UserId, 
                         Username = i.Username, 
                         CountContacts = i.CountContacts, 
                         CountBehaviorInterview = j.Any() ? j.Single().CountBehaviorInterview : 0
                      };

或者,使用其他方法(可能会更慢,具体取决于您的数据,但可能没有关系):

or, using a different method (may be slower, depending on your data, but probably doesn't matter):

var result = from i in contacts
             from o in interviews.Where(k => k.UserId == i.UserId).DefaultIfEmpty()
             select  new ContactsByUser
                     {
                         UserId = i.UserId, 
                         Username = i.Username, 
                         CountContacts = i.CountContacts, 
                         CountBehaviorInterview = o != null ? o.CountBehaviorInterview : 0
                      };

这篇关于如何合并两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆