【问题标题】:C# Linq Query for compare two tablesC# Linq Query 比较两个表
【发布时间】:2020-08-06 15:29:51
【问题描述】:

我希望 linq c# 查询比较两个表并列出表 1 中不匹配的记录

【问题讨论】:

标签: c# sql linq linq-to-sql


【解决方案1】:
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。

我们需要更多细节。

编辑

【讨论】:

  • 我有两张表,一张让我们拿一张表,它有一个名为“id”的列,它的行值如 1、2、3、4、5。和 B 表有列'id',它有像 3,4 这样的行值,我想在列表中只显示 1,2,5 的输出。使用 linq 查询
【解决方案2】:

最后一行代码比较两个列表的元素,并用 !包含仅保留第一个列表中不匹配的元素并添加到新的 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多