【问题标题】:Linq full outer join with NULL records C# from datatables来自数据表的带有 NULL 记录 C# 的 Linq 完全外连接
【发布时间】:2014-03-08 09:20:23
【问题描述】:

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

            //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

【问题讨论】:

标签: c# linq


【解决方案1】:

根据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);

【讨论】:

  • 感谢您对此提供的帮助,但仍在努力使其正常工作...任何帮助都会很棒。数据表 physextns = new DataTable(); extnlkp.Fill(physextns); //下面的表格格式 //("Company_Name") //("Phys_Switch_Name") //("Extn_Number") DataTable callrecdt = new DataTable(); callrec.Fill(callrecdt); //下面的表格格式 //("Switch_Name"); //("Extn_In_Call_Records"); //("Total_Calls"); //(“谈话时间”); //("Total_Cost");
猜你喜欢
  • 2014-09-14
  • 2018-05-24
  • 2018-05-30
  • 1970-01-01
  • 2014-08-07
  • 2017-06-10
  • 2018-03-04
  • 1970-01-01
  • 2015-06-23
相关资源
最近更新 更多