从两个对象列表创建一个列表

从两个对象列表创建一个列表

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

问题描述

我有以下情况

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

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

我需要将 2 个列表组合成一个新的 List如果它是同一个人,组合记录将具有该姓名,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 扩展联合方法中的第二个参数提供.有关如何编写此类比较器方法的更多信息,请访问 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 从两个对象列表创建一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 18:21