-3

私は次のクラスを持っています:

 public ObjectiveDetail()
    public int ObjectiveDetailId { get; set; }
    public int Number { get; set; }
    public string Text { get; set; }
    public override bool Equals(object obj)
    {
        return this.Equals(obj as ObjectiveDetail);
    }
    public bool Equals(ObjectiveDetail other)
    {
        if (other == null)
            return false;

        return this.Number.Equals(other.Number) &&
            (
                this.Text == other.Text ||
                this.Text != null &&
                this.Text.Equals(other.Text)
            );
    }
 }

2 つの ICollection コレクションがあります。

ICollection<ObjectiveDetail> _obj1; // Reference
ICollection<ObjectiveDetail> _obj2; // May have more, less or different objectDetails from the reference.

コレクションの共通フィールドはObjectiveDetailIdです。for ループを使用してコレクションをトラバースし、次の行を取得するにはどうすればよいですか。

  • _obj1 ではなく _obj2 にある
  • _obj2 ではなく _obj1 にある
  • _obj1 と _obj2 で異なる

これは以前に尋ねた別の質問に似ていますが、Equals メソッドを追加したので少し簡単になったと思います。

4

1 に答える 1

1

foreach を使用できます

foreach(ObjectiveDetail obj in _obj1)
{
    if (!(_obj2.Contains(obj)))
        //add to list
}

if ステートメントと ICollection のロジックを変更するだけで、残りの結果が得られます。

于 2013-08-10T14:20:26.323 に答える