C# 需要通过与'比较来筛选“dataBefore”的数据;dataAfter';
我有两个列表,需要在C# 需要通过与'比较来筛选“dataBefore”的数据;dataAfter';,c#,C#,我有两个列表,需要在dataAfter列表中筛选Name和SectionId不匹配的dataBefore中的数据 var dataBefore = new List<School> { new School { Name = "N1", SectionId = null }, new School { Name = "N2", SectionId = new Guid("
dataAfter
列表中筛选Name
和SectionId
不匹配的dataBefore
中的数据
var dataBefore = new List<School>
{
new School { Name = "N1", SectionId = null },
new School { Name = "N2", SectionId = new Guid("6aba7a38-8e61-472d-b4ce-b9fc2011af3f") },
new School { Name = "N3", SectionId = null },
new School { Name = "N4", SectionId = new Guid("ca663d45-04b8-4c80-96b6-c3760352a6ac") }
};
var dataAfter = new List<School>
{
new School { Name = "N1", SectionId = new Guid("5be0fc99-4826-4fbf-a190-b930af730b93") },
new School { Name = "N3", SectionId = null },
new School { Name = "N4", SectionId = null }
};
下面的查询给出了完全相反的结果(仅N3
),如何获得高于预期的结果
var x = dataBefore.Where(y => dataAfter.Any(z => z.Name == y.Name && z.SectionId == y.SectionId)).ToList();
如果Any()返回false,则可以得到结果
var x = dataBefore.Where(y => dataAfter.Any(z => z.Name == y.Name && z.SectionId == y.SectionId)).ToList();
var x = dataBefore.Where(y => !dataAfter.Any(z => z.Name == y.Name && z.SectionId == y.SectionId)).ToList();