从两个DataTable获取公共ID

时间:2012-11-29 07:35:02

标签: c# datatable

我有两个DataTable s:dt1& dt2dt1包含一个字段IDdt2包含两个字段ass_IDName

我必须从这两个ID中获取匹配的DataTable s的数量。我该怎么做呢?比较它们或任何东西以便在这两个表中获得匹配的ID s(公共ID s)的计数的任何简单方法?

2 个答案:

答案 0 :(得分:2)

var count = (from dr1 in dt.AsEnumerable()
            from dr2 in dt2.AsEnumerable()
            where dr1.Field<int>("ID") == dr2.Field<int>("ass_ID")
            select dr1).Count();

var count = (from dr1 in dt1.AsEnumerable()
             join j in dt2.AsEnumerable() on dr1.Field<int>("ID") equals j.Field<int>("ass_ID")
             select j).Count();

答案 1 :(得分:0)

试试这个:

string strExpression = string.Format("ID = '{0}'",dt2.Columns["ass_ID"]);
DafaultView dv = new DefaultView();
dv = dt1.DefaultView;
dv.RowFilter = strExpression;
//work with dv (DefaultView)