(一)Linq左连接案例分析
有如下两个DataTable:localDt和schemaDt
要实现这两个DataTable的左连接,得到如下效果:
分析一:


var temp1 = (from schema in schemaDt.AsEnumerable()
join local in localDt.AsEnumerable()
on schema.Field<string>("name") equals local.Field<string>("sql_name")
into tempTable
from item in tempTable.DefaultIfEmpty<DataRow>()
select item).ToList();
分析二


var temp2 = (from local in localDt.AsEnumerable()
join schema in schemaDt.AsEnumerable()
on local.Field<string>("sql_name") equals schema.Field<string>("name")
into tempTable
from item in tempTable.DefaultIfEmpty()
select item).ToList();
分析三:


代码var temp3 = (from schema in schemaDt.AsEnumerable() join local in localDt.AsEnumerable() on schema.Field<string>("name") equals local.Field<string>("sql_name") into tempTable from item in tempTable.DefaultIfEmpty<DataRow>(localDt.NewRow()) select item).ToList();
分析四:


代码var temp4 = (from schema in schemaDt.AsEnumerable() join local in localDt.AsEnumerable() on schema.Field<string>("name") equals local.Field<string>("sql_name") into tempTable from item in tempTable.DefaultIfEmpty<DataRow>(schemaDt.NewRow()) select item).ToList();