0

我如何比较 2 个列表?

public class Pers_Ordre : IEqualityComparer<Pers_Ordre>
{
    int _ordreId;
    public int LettreVoidID
    {
        get { return _LettreVoidID; }
        set { _LettreVoidID = value; }
    }

    string _OrdreCummul;
    public string OrdreCummul
    {
        get { return _OrdreCummul; }
        set { _OrdreCummul = value; }
    }

    // Products are equal if their names and product numbers are equal. 
    public bool Equals(Pers_Ordre x, Pers_Ordre 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;

        //Check whether the products' properties are equal. 
        return x.LettreVoidID == y.LettreVoidID && x.OrdreCummul == y.OrdreCummul;
    }

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

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

        //Get hash code for the Name field if it is not null. 
        int hashProductName = product.OrdreCummul == null ? 0 : product.OrdreCummul.GetHashCode();

        //Get hash code for the Code field. 
        int hashProductCode = product.LettreVoidID.GetHashCode();

        //Calculate the hash code for the product. 
        return hashProductName ^ hashProductCode;
    }
}

我这样比较:

private void simpleButton_Comparer_Click(object sender, EventArgs e)
{
    string LeFile_Client = System.IO.Path.Combine(appDir, @"FA.csv");
    string LeFile_Server = System.IO.Path.Combine(appDir, @"FA_Server.csv");

    List<Pers_Ordre> oListClient = Outils.GetCsv(LeFile_Client).OrderBy(t => t.LettreVoidID).ToList();
    List<Pers_Ordre> oListServert = Outils.GetCsvServer(LeFile_Server).OrderBy(t => t.LettreVoidID).ToList(); 

    List<Pers_Ordre> LeDiff = new List<Pers_Ordre>();

    LeDiff = oListServert.Except(oListClient).ToList();

    string Noid = "", OdreID = "";

    foreach (var oDiff in LeDiff)
    {
        Noid += oDiff.LettreVoidID + " ";
        OdreID += oDiff.OrdreCummul + " ";
    }

    MessageBox.Show(Noid + "--" + OdreID);
}

我无法得到正确的结果。

Lists 包含类对象,我们希望遍历一个列表,在第二个 List 中查找相同的项目并报告任何差异。

获取包含在列表 A 但不在列表 B 中的对象,反之亦然。

4

1 回答 1

1

您当前的.Except()调用将从服务器中查找客户端上缺少的项目,但不会在客户端上查找服务器上缺少的项目。

尝试这个:

private void simpleButton_Comparer_Click(object sender, EventArgs e)
{
    string LeFile_Client = System.IO.Path.Combine(appDir, @"FA.csv");
    string LeFile_Server = System.IO.Path.Combine(appDir, @"FA_Server.csv");

    var ListClient = Outils.GetCsv(LeFile_Client).OrderBy(t => t.LettreVoidID);
    var ListServer = Outils.GetCsvServer(LeFile_Server).OrderBy(t => t.LettreVoidID); 
    var LeDiff = ListServer.Except(ListClient).Concat(ListClient.Except(ListServer));

    var result = new StringBuilder();
    foreach (var Diff in LeDiff)
    {
        result.AppendFormat("{0} --{1} ", Diff.LettreVoidID, Diff.OrdreCummul);
    }
    MessageBox.Show(Noid.ToString() + "--" + OdreID);
}

此代码也应该比您的原始代码快得多,因为它避免在构建最终字符串之前将结果加载到内存中。此代码执行等效于两个单独的 sql LEFT JOIN。我们还可以通过一次 FULL JOIN 来加快速度,但这也需要编写我们自己的 linq 运算符方法。

于 2013-07-25T15:22:07.620 回答