LINQ full out join 实现

比如dt1数据是
id price 
1 100
2 100

dt2
id excelprice
1 100
3 200


想要得出来的数据是:
id price excleprice
1 100 100
2 100 null
3 null 100

 

如何实现?

首先想到的实现方式是:

 

[csharp]  view plain copy
  1. void Main()  
  2. {  
  3.     DataTable dtA = new DataTable();  
  4.         dtA.Columns.Add("id"typeof(int));  
  5.         dtA.Columns.Add("price"typeof(string));  
  6.         dtA.Rows.Add(1, "100");  
  7.         dtA.Rows.Add(2, "100");  
  8.        
  9.     DataTable dtB = dtA.Clone();  
  10.         dtB.Rows.Add(1, "100");   
  11.         dtB.Rows.Add(3, "100");  
  12.    
  13.     DataTable dtC = dtA.Clone();  
  14.         dtC.Columns.Add("price_excel");  
  15.   var fullJoinData =(from a in dtA.AsEnumerable()  
  16.                     join b in dtB.AsEnumerable()  
  17.                     on a.Field<int>("id") equals b.Field<int>("id") into g  
  18.                     from b in g.DefaultIfEmpty()  
  19.                     select new  
  20.                     {  
  21.                         id = a.Field<int>("id"),  
  22.                         price = a.Field<string>("price"),  
  23.                         price_excel = b == null ? "Null" : b.Field<string>("price")  
  24.                     }).Union  
  25.                     (  
  26.                         from  b in dtB.AsEnumerable()  
  27.                         join a in dtA.AsEnumerable()  
  28.                         on b.Field<int>("id") equals a.Field<int>("id") into g  
  29.                         from a in g.DefaultIfEmpty()  
  30.                         select new  
  31.                         {  
  32.                             id = b.Field<int>("id"),  
  33.                             price = a == null ? "Null" : a.Field<string>("price"),  
  34.                             price_excel =b.Field<string>("price")   
  35.                         }  
  36.                     );  
  37.                       
  38.  fullJoinData.ToList().ForEach(q => dtC.Rows.Add(q.id, q.price, q.price_excel));  
  39.  }  


 

后来写了一个条理比较清晰的实现方式:

 

[csharp]  view plain copy
  1. void Main()  
  2. {  
  3.     DataTable dtA = new DataTable();  
  4.         dtA.Columns.Add("id"typeof(int));  
  5.         dtA.Columns.Add("price"typeof(string));  
  6.         dtA.Rows.Add(1, "100");  
  7.         dtA.Rows.Add(2, "100");  
  8.        
  9.     DataTable dtB = dtA.Clone();  
  10.         dtB.Rows.Add(1, "100");   
  11.         dtB.Rows.Add(3, "100");  
  12.    
  13.     DataTable dtC = dtA.Clone();  
  14.         dtC.Columns.Add("price_excel");  
  15.           
  16.     var leftData=from a in dtA.AsEnumerable()  
  17.                     join b in dtB.AsEnumerable()  
  18.                     on a.Field<int>("id") equals b.Field<int>("id") into g  
  19.                     from b in g.DefaultIfEmpty()  
  20.                     select new  
  21.                     {  
  22.                         id = a.Field<int>("id"),  
  23.                         price = a.Field<string>("price"),  
  24.                         price_excel = b == null ? "Null" : b.Field<string>("price")  
  25.                     };  
  26.     var rightData=from b in dtB.AsEnumerable()  
  27.                   where  !dtA.AsEnumerable().Select(a=>a.Field<int>("id")).Contains(b.Field<int>("id"))  
  28.                   select new   
  29.                     {  
  30.                             id = b.Field<int>("id"),  
  31.                             price = "Null",  
  32.                             price_excel =b.Field<string>("price")   
  33.                     };  
  34.           
  35.  var fullJoinData =leftData.Union(rightData);  
  36.  fullJoinData.ToList().ForEach(q => dtC.Rows.Add(q.id, q.price, q.price_excel));  
  37.   
  38. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值