使用linq从两个对象列表创建一个列表 [英] Create a list from two object lists with linq

查看:61
本文介绍了使用linq从两个对象列表创建一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况

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

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

我需要将2个列表合并为一个新的List<Person> 如果合并记录是同一个人,则合并记录将具有该名称,即list2中个人的值,更改将为list2的值-list1的值. 如果没有重复,则更改为0

I need to combine the 2 lists into a new List<Person> 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扩展方法Union轻松完成.例如:

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

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

这将返回一个列表,其中两个列表被合并,而双精度项被删除.如果未在我的示例中的Union扩展方法中指定比较器,它将在Person类中使用默认的Equals和GetHashCode方法.例如,如果您想通过比较人员的Name属性来比较人员,则必须重写这些方法才能自己执行比较.检查以下代码示例以完成此操作.您必须将此代码添加到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类的默认Equals方法设置为始终使用Name来比较两个对象,则还可以编写一个使用IEqualityComparer接口的比较器类.然后,您可以将此比较器作为Linq扩展Union方法中的第二个参数提供.有关如何编写这种比较器方法的更多信息,请参见 http ://msdn.microsoft.com/zh-CN/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天全站免登陆