合并使用LINQ两个目标清单 [英] Merge two object lists with linq

查看:87
本文介绍了合并使用LINQ两个目标清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的情况

class Person
{
    string Name;
    int Value;
    int Change;
}

List<Person> list1;
List<Person> list2;

我需要2个名单组合成一个新的列表
万一这是同一个人相结合的记录将有一个名称,人列表2的值,变化是list2中的价值 - list1的价值。
更改为0,如果没有重复

I need to combine the 2 lists into a new List in case it's the same person the combine record would have that name, value of the person in list2, change would be the value of list2 - the value of list1. Change is 0 if no duplicate

推荐答案

这可以很容易地通过使用LINQ的扩展方法做联盟。例如:

This can easily be done by using the Linq extension method Union. For example:

var mergedList = list1.Union(list2).ToList();

这将返回在两个列表合并和双打被删除列表。如果你没有在我的例子中指定就像在联盟扩展方法的比较器,它会使用默认的Equals和GetHash code方法,您的Person类。比如你想通过比较它们的名称属性进行比较的人,必须重写这些方法自己进行比较。检查下面的code样品来实现这一目标。必须添加此code到您的Person类。

This will return a List in which the two lists are merged and doubles are removed. If you don't specify a comparer in the Union extension method like in my example, it will use the default Equals and GetHashCode methods in your Person class. If you for example want to compare persons by comparing their Name property, you must override these methods to perform the comparison yourself. Check the following code sample to accomplish that. You must add this code to your Person class.

/// <summary>
/// Checks if the provided object is equal to the current Person
/// </summary>
/// <param name="obj">Object to compare to the current Person</param>
/// <returns>True if equal, false if not</returns>
public override bool Equals(object obj)
{        
    // Try to cast the object to compare to to be a Person
    var person = obj as Person;

    return Equals(person);
}

/// <summary>
/// Returns an identifier for this instance
/// </summary>
public override int GetHashCode()
{
    return Name.GetHashCode();
}

/// <summary>
/// Checks if the provided Person is equal to the current Person
/// </summary>
/// <param name="personToCompareTo">Person to compare to the current person</param>
/// <returns>True if equal, false if not</returns>
public bool Equals(Person personToCompareTo)
{
    // Check if person is being compared to a non person. In that case always return false.
    if (personToCompareTo == null) return false;

    // If the person to compare to does not have a Name assigned yet, we can't define if it's the same. Return false.
    if (string.IsNullOrEmpty(personToCompareTo.Name) return false;

    // Check if both person objects contain the same Name. In that case they're assumed equal.
    return Name.Equals(personToCompareTo.Name);
}

如果您不想设置默认等于你的Person类总是使用名称来比较两个对象的方法,也可以写它使用的IEqualityComparer接口的比较器类。然后,您可以提供这个比较器作为LINQ的扩展方法联盟的第二个参数。如何写这样的比较器方法的详细信息可以在<一个找到href=\"http://msdn.microsoft.com/en-us/library/system.collections.iequalitycomparer.aspx\">http://msdn.microsoft.com/en-us/library/system.collections.iequalitycomparer.aspx

If you don't want to set the default Equals method of your Person class to always use the Name to compare two objects, you can also write a comparer class which uses the IEqualityComparer interface. You can then provide this comparer as the second parameter in the Linq extension Union method. More information on how to write such a comparer method can be found on http://msdn.microsoft.com/en-us/library/system.collections.iequalitycomparer.aspx

这篇关于合并使用LINQ两个目标清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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