(一)Linq左连接案例分析
有如下两个DataTable:localDt和schemaDt
![image image](https://images2018.cnblogs.com/blog/329170/201806/329170-20180625015012365-375563431.png)
![image image](https://images2018.cnblogs.com/blog/329170/201806/329170-20180625015012763-1401933691.png)
要实现这两个DataTable的左连接,得到如下效果:
分析一:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
代码
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();
![image image](https://images2018.cnblogs.com/blog/329170/201806/329170-20180627122341364-963409289.png)
分析二
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
代码
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();
分析三:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
代码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();
![image image](https://images2018.cnblogs.com/blog/329170/201806/329170-20180625015013766-835142320.png)
分析四:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
代码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();
![image image](https://images2018.cnblogs.com/blog/329170/201806/329170-20180625015014153-2113303139.png)