比较两个列表<;T>;

本文关键字:lt gt 列表 两个 比较 | 更新日期: 2023-09-27 18:24:59

如何比较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包含类对象,我们希望遍历一个列表,在第二个列表中查找相同的项并报告任何差异。

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

比较两个列表<;T>;

您当前的.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);
}

这段代码也应该比原始代码快得多,因为它避免了在构建最终字符串之前将结果加载到内存中。中的此代码执行两个独立的sqlLEFTJOIN的等效操作。我们可以通过执行一个FULLJOIN来使它更快,但这也需要编写我们自己的linq运算符方法。