c# - 查询找出两个数据表中重复的行?

标签 c# asp.net sql-server datatable match

我有一个包含列的 Excel 工作表:

Id,Data

我在 sql server 数据库中有一个表:

Id,Data

我在两个不同的 DataTables 中派生了行,现在我想检测 excel 和数据库表中常见的行,并将重复的行存储在另一个 DataTable 中. 像这样:

OleDbCommand command1 = new OleDbCommand("select * from [Sheet1$]",connection);
DataTable dt2 = new DataTable();
try
{
    OleDbDataAdapter da2 = new OleDbDataAdapter(command1);
    da2.Fill(dt2);
}
catch { }

最佳答案

尝试像这样使用 linq 方式

var matched = from table1 in dt1.AsEnumerable()
               join table2 in dt2.AsEnumerable() on 
             table1.Field<int>("ID") equals table2.Field<int>("ID")
             where table1.Field<string>("Data") == table2.Field<string>("Data")
             select table1;

关于c# - 查询找出两个数据表中重复的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13062720/

相关文章:

sql-server - 在这种情况下 READ UNCOMMITTED/NOLOCK 安全吗?

php - 在 WAMP 上为 PHP 安装 SQLSRV 扩展

C#:通过方法在构造函数中设置属性

c# - 我如何在 C# 中使用 XNA 来减少碰撞时的生命周期?

c# - ListView 控件中的分页

asp.net - 更改 varchar 最大长度后,我是否必须更新 Entity Framework

c# - .OrderBy 函数的 System.Linq.Expressions.Expression

javascript - 数据保存后、重定向到同一页面(或刷新)之前显示“确定”对话框

sql-server - SQL Server 中的记录级安全性

c# - 使用 DataTemplate 双击项目的列表框