本文介绍了LEFT OUTER JOIN 2数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想了解如何查询,最好用LINQ,2数据表。
我愿做一个LEFT OUTER JOIN他们
I'm trying to understand how to query, preferably with LINQ, 2 datatables.I would like to do a LEFT OUTER JOIN on them
Datatable1与COL的:[ID] [可口可乐]
DataTable2与山坳的:。[ID] [COLB] [COLC] ...
Datatable1 with col's: [ID] [colA]
DataTable2 with col's: [ID] [ColB] [ColC] ...
希望加入上ID
是否有人可以告诉我一个例子,所以我可以将其应用到数据表我有吗?谢谢你在前进
Can someone please show me an example so I can apply it to the datatables I have? Thank you in advance
推荐答案
这编译并执行你根据给定结果集所期望的:
This compiles and does what you would expect based on the result set given:
// create the default row to be used when no value found
var defaultRow = DataTable2.NewRow();
defaultRow[0] = 0;
defaultRow[1] = String.Empty;
// the query
var result = from x in DataTable1.AsEnumerable()
join y in DataTable2.AsEnumerable() on (string)x["ID"] equals (string)y["ID"]
into DataGroup
from row in DataGroup.DefaultIfEmpty<DataRow>(defaultRow)
select new {a = x["ColA"], b = (string)row["ColB"]};
这篇关于LEFT OUTER JOIN 2数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!