如何将数据表识别为另一个数据表的一部分?
How to recognize a datatable as a part of another datatable?
如何操作:
我有两个 datatables
:
FIRST : DT_All
---> composed of : emp_num
,....other columns
SECOND :DT_Part
--->composed of one column as a key emp_num
其中 DT_part
可能是 DT_ALL
的一部分或不存在于此数据表中。
如果它是 DT_All
的一部分
我想要一种在 DT_ALL
中识别它们的方法。如何简单快速地做到这一点?
示例:
DT_All
emp_num column1 column2 column3
227 7 33.3 ss
155 5 10.7 mm
122 5 1.66 aa
678 2 8.9 rr
555 1 1.11 aa
DT_part
:
emp_num
155
678
我想要这样的输出:
emp_num column1 column2 column3 flag
227 7 33.3 ss 0
155 5 10.7 mm 1
122 5 1.66 aa 0
678 2 8.9 rr 1
555 1 1.11 aa 0
您可以使用 Contains
运算符。下面的伪代码(未经测试,可能会出现一些错别字):
var items = DT_All
.AsEnumerable()
.Where(x => DT_Part.Field<string>("emp_num").Contains(x.EmpNum));
你可以像这样在两个表上做一个 left join
:-
var result = from t1 in dt1.AsEnumerable()
join t2 in dt2.AsEnumerable()
on t1.Field<int>("emp_num") equals t2.Field<int>("emp_num") into g
from foo in g.DefaultIfEmpty()
select new
{
emp_num = t1.Field<int>("emp_num"),
col1 = t1.Field<string>("col1"),
flag = foo != null ? 1 : 0
};
如何操作:
我有两个 datatables
:
FIRST :
DT_All
---> composed of :emp_num
,....other columnsSECOND :
DT_Part
--->composed of one column as a keyemp_num
其中 DT_part
可能是 DT_ALL
的一部分或不存在于此数据表中。
如果它是 DT_All
我想要一种在 DT_ALL
中识别它们的方法。如何简单快速地做到这一点?
示例:
DT_All
emp_num column1 column2 column3
227 7 33.3 ss
155 5 10.7 mm
122 5 1.66 aa
678 2 8.9 rr
555 1 1.11 aa
DT_part
:
emp_num
155
678
我想要这样的输出:
emp_num column1 column2 column3 flag
227 7 33.3 ss 0
155 5 10.7 mm 1
122 5 1.66 aa 0
678 2 8.9 rr 1
555 1 1.11 aa 0
您可以使用 Contains
运算符。下面的伪代码(未经测试,可能会出现一些错别字):
var items = DT_All
.AsEnumerable()
.Where(x => DT_Part.Field<string>("emp_num").Contains(x.EmpNum));
你可以像这样在两个表上做一个 left join
:-
var result = from t1 in dt1.AsEnumerable()
join t2 in dt2.AsEnumerable()
on t1.Field<int>("emp_num") equals t2.Field<int>("emp_num") into g
from foo in g.DefaultIfEmpty()
select new
{
emp_num = t1.Field<int>("emp_num"),
col1 = t1.Field<string>("col1"),
flag = foo != null ? 1 : 0
};