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);
}

正しい結果が得られません。

リストにはクラス オブジェクトが含まれており、1 つのリストを反復処理して、2 番目のリストで同じ項目を探し、相違点を報告したいと考えています。

リスト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);
}

このコードは、最終的な文字列を作成するまで結果をメモリにロードしないため、元のコードよりも大幅に高速になります。このコードは、2 つの個別の SQL LEFT JOIN と同等の機能を実行します。FULL JOIN を 1 回実行することでさらに高速化できますが、それには独自の linq 演算子メソッドも作成する必要があります。

于 2013-07-25T15:22:07.620 に答える