C#:获取两个结果集的差集

前端之家收集整理的这篇文章主要介绍了C#:获取两个结果集的差集前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
两个结果集数据格式相同,数据结构不同

1. 类定义

public class TestClassA
{
    public string Code { get; set; }
public string Name { get; set; }

}

public class TestClassB
{
public string UserCode { get; set; }

public string UserName { get; set; }

}

2.填充数据

var testClassAList = new List
{
    new TestClassA { Name="小王",Code = "001"},new TestClassA { Name="小王",Code = "002"},new TestClassA { Name="老王",Code = "003"},new TestClassA { Name="小张",Code = "001"}
};

var testClassBList = new List
{
new TestClassB { UserName="小王",UserCode = "001"},new TestClassB { UserName="小张",UserCode = "002"}
};

3.具体实现

var differentList = classAList.Where(l => !classBList.Any(t => t.UserName == l.Name && t.UserCode == l.Code));

foreach (var item in differentList)
{
Console.WriteLine($"Name: {item.Name} | Code: {item.Code}");
}

原文链接:https://www.f2er.com/note/420995.html

猜你在找的程序笔记相关文章