【发布时间】:2020-08-06 15:29:51
【问题描述】:
我希望 linq c# 查询比较两个表并列出表 1 中不匹配的记录
【问题讨论】:
-
您好,请阅读stackoverflow.com/help/how-to-ask 我们需要更多信息才能为您提供帮助
标签: c# sql linq linq-to-sql
我希望 linq c# 查询比较两个表并列出表 1 中不匹配的记录
【问题讨论】:
标签: c# sql linq linq-to-sql
List<MyClass> Unmatched = new List<MyClass>();
foreach (var row in Table_A)
{
if (Table_B.Count(x => x.ID == row.ID) == 0)
Unmatched.Add(row);
}
类似的东西?
它只会检查 Unmached Table1 到 Table2。 它不会检查 Table2 到 Table1。
我们需要更多细节。
【讨论】:
最后一行代码比较两个列表的元素,并用 !包含仅保留第一个列表中不匹配的元素并添加到新的 Unmathced 列表中:
List<string> table_1 = new List<string>() { "panos", "mitsos", "alex", "niki" };
List<string> table_2 = new List<string>() { "panos", "mitsos", "alexandros", "maria" };
List<string> UnmatchedList= new List<string>();
UnmatchedList = table_1.Where(x => !table_2.Contains(x)).ToList();
【讨论】: