来自数据表的具有空记录C#的Linq完全外部联接

来自数据表的具有空记录C#的Linq完全外部联接,c#,linq,C#,Linq,有人能帮忙吗?我需要在Extn_In_Call_Records=Extn_Number上返回一个表,如果任何一方不匹配,仍然返回一个计算,就像SQL完全外部联接一样。我已经花了几个小时看这个,但无法让它工作!!如果我删除联合,我可以让下面的代码工作,但它只返回匹配的结果。正在从MYSQL填充Datatable。任何帮助都会很好 //Full Table DataTable fullext = new DataTable(); fullext

有人能帮忙吗?我需要在Extn_In_Call_Records=Extn_Number上返回一个表,如果任何一方不匹配,仍然返回一个计算,就像SQL完全外部联接一样。我已经花了几个小时看这个,但无法让它工作!!如果我删除联合,我可以让下面的代码工作,但它只返回匹配的结果。正在从MYSQL填充Datatable。任何帮助都会很好

            //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
根据,执行完全外部联接的最简单方法是合并两个左联接。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);

请参阅(有关LINQ中的一般“外部”概念),并感谢您在这方面的帮助,但仍在努力使其正常工作。。。任何帮助都会很好。DataTable physextns=新DataTable();外部填充(物理性)//下表格式//(“公司名称”)/(“物理开关名称”)/(“外部编号”)数据表callrecdt=new DataTable();callrec.Fill(callrecdt)//下表格式//(“开关名称”)//(“Extn_In_Call_Records”)//(“总催缴股款”)//(“谈话时间”)//(“总成本”);