假設我有一個Person
類,如下所示,其中有兩個Person
集合實例,第一個實例是包含所有記錄的主集合,而第二個實例實例是主集合的一個子集。C#Linq - 匹配兩組數據並更新匹配的第一組
我需要做的是更新主集的IsMatched屬性,其中實例也存在於子集中。
public class Person
{
public int Id { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsMatched { get; set; }
}
基於Tim的建議我創建一個單元測試來測試解決方案
[TestFixture()]
public class Test
{
[TestFixture]
public class MatchedSetTest
{
private class Person
{
public int Id { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsMatched { get; set; }
}
[Test]
public void TestMatchingSets()
{
var members = new List<Person>()
{
new Person() { FirstName = "Tom", LastName = "Smith", Id = 1, Title = "Mr", IsMatched = false} ,
new Person() { FirstName = "Paul", LastName = "Jones", Id = 2, Title = "Mr", IsMatched = false} ,
new Person() { FirstName = "Gary", LastName = "Thompson", Id = 3, Title = "Mr", IsMatched = false} ,
new Person() { FirstName = "Simon", LastName = "Green", Id = 4, Title = "Mr", IsMatched = false} ,
new Person() { FirstName = "Phil", LastName = "Stuart", Id = 5, Title = "Mr", IsMatched = false} ,
new Person() { FirstName = "Sean", LastName = "Appleton", Id = 6, Title = "Mr", IsMatched = false}
};
var buddy = new List<Person>()
{
new Person() { FirstName = "Tom", LastName = "Smith", Id = 1, Title = "Mr", IsMatched = false} ,
new Person() { FirstName = "Gary", LastName = "Thompson", Id = 3, Title = "Mr", IsMatched = false} ,
new Person() { FirstName = "Simon", LastName = "Green", Id = 4, Title = "Mr", IsMatched = false}
};
var existing = from m in members
join s in buddy on m.Id equals s.Id
select new { Master = m, Subset = s };
foreach (var both in existing)
{
both.Master.IsMatched = both.Subset.IsMatched;
}
var p = existing.Where(w => w.Master.Id == 1).FirstOrDefault().Master;
Assert.IsTrue(p.IsMatched);
}
}
}
現有的對象包含與內的主機和子集實例和所有實例IsMatched性能從子集的三個項目仍然假。
我需要的是整個主集合返回時,匹配的主集合實例上的子集中的匹配項將IsMatched屬性標記爲true。
另一個選項
這會是一個合適的解決方案?
buddy.ForEach(b =>
{
var member = members.FirstOrDefault(w => w.Id == b.Id);
if (member != null)
member.IsMatched = true;
});
多少項做這些集合包含哪些內容? – Fabjan
主集包含大約9000行,而子集可能包含50-100 –