2014-02-10 40 views
3

请有人可以帮忙吗?我需要在Extn_In_Call_Records = Extn_Number上返回一个表,如果任何一方不匹配,就像SQL Full Outer join一样返回一个calue。我花了几个小时看这个,但不能让它工作!如果我删除了union,但是它只返回匹配的结果,我可以在下面得到代码。 Datatable正在从MYSQL中填充。任何帮助都会很棒。Linq完全外部连接与NULL记录C#从数据表

  //Full Table 
     DataTable fullext = new DataTable(); 
     fullext.Columns.Add("Extn_In_Call_Records", typeof(string)); 
     fullext.Columns.Add("Total_Calls", typeof(int)); 
     fullext.Columns.Add("Extn_Number", typeof(string)); 
     fullext.Columns.Add("Phys_Switch_Name", typeof(string)); 


     //End Full Table 


     try 
     { 

      //Full Result 


      var result = from callrc in callrecdt.AsEnumerable() 
          join physex in physextns.AsEnumerable() 
          on callrc["Extn_In_Call_Records"] equals physex["Extn_Number"] 
          .Union 
          from physex in physextns.AsEnumerable() 
          join callrc in callrecdt.AsEnumerable() 
          on physex["Extn_Number"] equals callrc["Extn_In_Call_Records"] 



          select fullext.LoadDataRow(new object[] { 
         callrc["Extn_In_Call_Records"], 
         callrc["Total_Calls"], 
         physex["Extn_Number"] == null ? "" : physex["Extn_Number"], 
         physex["Phys_Switch_Name"] == null ? "" : physex["Phys_Switch_Name"] 
         }, false); 
      result.CopyToDataTable(); 
      fullresult.DataSource = fullext; 

结果看

Extn_In_Call_Records Total_Calls Extn_Number  Phys_Switch_Name 
null     20    0    Hospital 
null     310    1    Hospital 
4      132    4    Hospital 
2004     null    null   Hospital 
2006     2    2006   Hospital 
+0

见http://stackoverflow.com/questions/ 1122942/linq-to-sql-left-outer-join-with-multiple-join-conditions?rq = 1(对于LINQ中的一般“外部”概念)和http://stackoverflow.com/questions/5489987/linq -full-outer-join?rq = 1 – user2864740

回答

4

LINQ - Full Outer Join,执行完整外最简单的方法是加入工会两个左联接。阿左加入LINQ(使用扩展方法的语法)的形式为:

var leftJoined = from left in lefts 
       join right in rights 
        on left.Key equals right.Key 
       into temp 
       from newRight in temp.DefaultIfEmpty(/* default value for right */) 
       select new 
       { 
        /* use left and newRight to construct the joined object */ 
       } 

在你的情况,你想做的事:

// initialize some default elements to use later if 
// they're of the same type then a single default is fine 
var defaultPhysex = new {...}; 
var defaultCallrc = new {...}; 

var left = from callrc in callrecdt.AsEnumerable() 
      join physex in physextns.AsEnumerable() 
      on callrc["Extn_In_Call_Records"] equals physx["Extn_Number"] 
      into temp 
      from physex in temp.DefaultIfEmpty(defaultPhysex) 
      select new 
      { 
       // callrc is accessible here, as is the new physex 
       Field1 = ..., 
       Field2 = ..., 
      } 

var right = from physex in physextns.AsEnumerable() 
      join callrc in callrecdt.AsEnumerable() 
       on callrc["Extn_In_Call_Records"] equals physx["Extn_Number"] 
      into temp 
      from callrc in temp.DefaultIfEmpty(defaultCallrc) 
      select new 
      { 
       // physex is accessible here, as is the new callrc 
       Field1 = ..., 
       Field2 = ..., 
      } 

var union = left.Union(right); 
+0

谢谢你对此的帮助,但仍然努力工作......任何帮助都会很棒。 DataTable physextns = new DataTable(); extnlkp.Fill(physextns); //表格格式下面 \t \t \t //( “COMPANY_NAME”) //( “Phys_Switch_Name”) //( “Extn_Number”) 数据表callrecdt =新数据表(); callrec.Fill(callrecdt); //表格格式在 以下\t \t \t //(“Switch_Name”); //(“Extn_In_Call_Records”); //(“Total_Calls”); //(“Talk_Time”); //(“Total_Cost”); –