c# - 怎样做才能只得到一个列表?

标签 c# list

你好,我有一个方法可以比较 2 个列表的对象是否存在差异。目前,这种方法有效,但一次仅适用于一处特性。

方法如下:

    public SPpowerPlantList compareTwoLists(string sqlServer, string database, DateTime timestampCurrent, string noteCurrent, DateTime timestampOld, string noteOld)
    {

        int count = 0;
        SPpowerPlantList powerPlantListCurrent = loadProjectsAndComponentsFromSqlServer(sqlServer, database, timestampCurrent, noteCurrent);
        SPpowerPlantList powerPlantListOld = loadProjectsAndComponentsFromSqlServer(sqlServer, database, timestampOld, noteOld);
        SPpowerPlantList powerPlantListDifferences = new SPpowerPlantList();
        count = powerPlantListOld.Count - powerPlantListCurrent.Count;

        var differentObjects = powerPlantListCurrent.Where(p => !powerPlantListOld.Any(l => p.mwWeb == l.mwWeb)).ToList();

        foreach (var differentObject in differentObjects)
        {
            powerPlantListDifferences.Add(differentObject);
        }

        return powerPlantListDifferences;

    }

这有效,我在新列表中得到 4 个对象。问题是我还有一些其他属性需要比较。而不是 mwWeb 作为示例名称。当我尝试更改它时,我需要为每个新属性一个新的列表和一个新的 Foreach 循环。

例如

    int count = 0;
    SPpowerPlantList powerPlantListCurrent = loadProjectsAndComponentsFromSqlServer(sqlServer, database, timestampCurrent, noteCurrent);
    SPpowerPlantList powerPlantListOld = loadProjectsAndComponentsFromSqlServer(sqlServer, database, timestampOld, noteOld);
    SPpowerPlantList powerPlantListDifferences = new SPpowerPlantList();
    SPpowerPlantList powerPlantListDifferences2 = new SPpowerPlantList();

    count = powerPlantListOld.Count - powerPlantListCurrent.Count;

    var differentObjects = powerPlantListCurrent.Where(p => !powerPlantListOld.Any(l => p.mwWeb == l.mwWeb)).ToList();
    var differentObjects2 = powerPlantListCurrent.Where(p => !powerPlantListOld.Any(l => p.shortName == l.shortName)).ToList();

    foreach (var differentObject in differentObjects)
    {
        powerPlantListDifferences.Add(differentObject);
    }

    foreach (var differentObject in differentObjects2)
    {
        powerPlantListDifferences2.Add(differentObject);
    }

    return powerPlantListDifferences;

有办法防止这种情况发生吗?或者进行更多查询并仅获取 1 个包含所有不同对象的列表?

我尝试使用 exceptintersect 但没有成功。 因此,任何帮助或建议都会很棒,感谢您的宝贵时间。

PS:如果我的问题风格有问题,请告诉我,因为我尝试学习提出更好的问题。

最佳答案

您可以简单地将您想要在 Where() 中比较的属性链接起来。使用 OR 语句的子句:

// This should get you any elements that have different A properties, B properties, etc.
var different = current.Where(p => !old.Any(l => p.A == l.A || p.B == l.B))
                       .ToList();

如果这不起作用并且您确实想使用 Except() Intersect() 方法来正确比较对象,您可以编写自己的自定义 IEqualityComparer<YourPowerPlant> 用于正确比较它们:

class PowerPlantComparer : IEqualityComparer<YourPowerPlant>
{
    // Powerplants are are equal if specific properties are equal. 
    public bool Equals(YourPowerPlant x, YourPowerPlant y)
    {
        // Check whether the compared objects reference the same data. 
        if (Object.ReferenceEquals(x, y)) return true;

        //Check whether any of the compared objects is null. 
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        // Checks the other properties to compare (examples using mwWeb and shortName)
        return x.mwWeb == y.mwWeb && x.shortName == y.shortName;
    }

    // If Equals() returns true for a pair of objects  
    // then GetHashCode() must return the same value for these objects. 

    public int GetHashCode(YourPowerPlant powerPlant)
    {
        // Check whether the object is null 
        if (Object.ReferenceEquals(powerPlant, null)) return 0;

        // Get hash code for the mwWeb field if it is not null. 
        int hashA = powerPlant.mwWeb == null ? 0 : powerPlant.mwWeb.GetHashCode();

        // Get hash code for the shortName field if it is not null. 
        int hashB = powerPlant.shortName == null ? 0 : powerPlant.shortName.GetHashCode();

        // Calculate the hash code for the product. 
        return hashA ^ hashB;
    }
}

然后您可能会根据您的需要使用类似以下内容之一:

var different = current.Except(old,new PowerPlantComparer());

或:

var different = current.Intersect(old,new PowerPlantComparer());

关于c# - 怎样做才能只得到一个列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36915190/

相关文章:

c# - NHibernate 模式更新

java - 如何获取 Java Stream 中每种类型(按属性选择)的第一个对象(按函数排序)

java - Android ListView 删除所有内容后仍然包含一项

android - 带有复选框问题的自定义 ListView

python - .join() 在 python 中给出返回列表

c# - "this"有什么用?

c# - 在 Windows Phone 8 中使用 HttpClient 执行 POST 时出现内部服务器错误

c# - 如何将具有特殊顺序的特定文本框保存到文本文件中?

python - 在python中读取文件后返回单词列表

c# - 使用C#计算PDF文档中减号的数量