-1

我有两个通用列表。他们的领域大多不同,但有 4 个领域有共同点。我想使用这四个字段作为“平等”的定义来获取其中一个列表中的项目列表,而不是另一个列表中的项目列表。

这是我解决问题的尝试。

var unMatchedData = from liveLines in liveList
    join oldList in comapreSnapshotList
    on new {liveLines.ClientNo, liveLines.SequenceNo, liveLines.LineNo, liveLines.Text} equals
    new {oldList.ClientNo, oldList.SequenceNo, oldList.LineNo, oldList.Text}
    select new KNOWTXTS
    {
        ClientNo = liveLines.ClientNo,
        SequenceNo = liveLines.SequenceNo,
        LineNo = liveLines.LineNo,
        Text = liveLines.Text
    };
4

1 回答 1

1

您可以使用Except来查找设置的差异。

var newElements = liveList.Select(l => new {
                      l.ClientNo, l.SequenceNo, l.LineNo, l.Text
                  });
var oldElements = comapreSnapshotList.Select(l => new {
                      l.ClientNo, l.SequenceNo, l.LineNo, l.Text
                  });
var newElementsInNew = newElements.Except(oldElements);
var deletedFromNew   = oldElements.Except(newElements);


// if you need the original object in the list 
var newElements = from obj in liveList 
                  join newEle in newElementsInNew 
                  on new {obj.ClientNo, obj.SequenceNo, obj.LineNo, obj.Text} equals newEle
                  select obj;
var deletedElements = from obj in comapreSnapshotList
                  join deletedEle in deletedFromNew   
                  on new {obj.ClientNo, obj.SequenceNo, obj.LineNo, obj.Text} equals deletedEle
                  select obj;
于 2012-09-13T15:36:20.093 回答