【问题标题】:Getting the Rows from DataTable1 where they are not in DataTable2从 DataTable1 中获取不在 DataTable2 中的行
【发布时间】:2012-07-25 02:44:30
【问题描述】:

我有 2 个数据表

DataTable1
SNo
1
2
3
4

DataTable2

SNo
1
4
3

I want the result

DataTableResult

SNo
2

我可以使用for 循环来做到这一点。

但我正在尝试使用 linq。

var intersection = DataTable1.AsEnumerable().Intersect(DataTable2.AsEnumerable(), DataRowComparer.Default);    

然后我发现 Intersect 会给你两个表都有的行。

我不知道要使用什么功能。

有什么想法吗?

我已经知道答案了。

感谢删除答案的人。

但这给了我一个开始。

答案是

var intersection = DataTable1.AsEnumerable().Except(DataTable2.AsEnumerable(), DataRowComparer.Default);

【问题讨论】:

    标签: asp.net linq datatable


    【解决方案1】:

    您可以尝试使用 exept 语句,因为您只将表格显示为结构,但您可以尝试这样的想法

     var qry1 = datatable1.AsEnumerable().Select(a => new { SNo.ToString() });
     var qry2 = datatable2.AsEnumerable().Select(b => new { SNo.ToString() });
     var exceptAB = qry1.Except(qry2);
    

    【讨论】:

      【解决方案2】:

      你可以像这样使用查询

      select SNo from DataTable1 where SNo not in (select SNo from DataTable2);
      

      【讨论】:

        猜你喜欢
        • 2015-07-07
        • 2014-05-24
        • 2019-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多