全外连接Linq

时间:2014-01-27 10:15:18

标签: c# sql linq

我确实离开了加入并且正确加入然后做完全外连接的联合这里是我的代码

var brndT = (from a in db.TSA_TARGET_DETAIL 
                         where a.OUTLET_ID == id && a.CAMPAIGN_ID == campaignId && a.EMPLOYEE_ID == empId 
                         select new 
                         {
                             ID=a.BRAND_ID,
                             Target=a.STICK_QTY
                         }).ToList();

            var brndS = (from p in db.SR_TRN_DETAILS
                      where
                          (from ppt in db.SR_TRN_MAIN
                           where
                           ppt.MEMO_DATE >= d2 && ppt.MEMO_DATE <= d1 && ppt.OUTLET_ID==id 
                           select ppt.ORDER_ID).Contains(p.ORDER_ID)
                      group p by p.BRAND_ID into g
                      select new
                      {
                          ID = g.Key,
                          Qty = g.Select(x => x.QUANTITY).Sum()
                      }).ToList();
            var left = (from T in brndT
                       join S in brndS
                       on T.ID equals S.ID
                       into temp
                       from S in temp.DefaultIfEmpty()
                       select new BrandSalesTarget
                       {
                           ID=T.ID,
                           Target = T.Target==null?0:(int)T.Target,
                           Sales = S != null ? (int)S.Qty : 0,
                       }).ToList();
            var right = (from S in brndS
                         join T in brndT
                         on S.ID equals T.ID
                         into temp
                         from T in temp.DefaultIfEmpty()
                         select new BrandSalesTarget
                         {
                             ID=S.ID,
                             Sales = S.Qty==null?0:(int)S.Qty,
                             Target = T != null ? (int)T.Target : 0
                         }).ToList();
            var fullOuter = left.Union(right).ToList();

但是工会不起作用。我两次得到相同的id。我的代码出了什么问题?任何帮助非常感谢。提前谢谢。

1 个答案:

答案 0 :(得分:0)

尝试使用IEqualityComparer,以便联合知道哪些元素是相同的。

借来的代码。 union in two linq statements and remove the duplicate

class FirstElementComparer : IEqualityComparer<string[]>
{
    //TODO error checking
    public bool Equals(string[] a, string[] b)
    {       
        return a[0].Equals(b[0]);
    }

    public Int32 GetHashCode(string[] obj)
    {
        return obj[0].GetHashCode();
    }
}

并像这样使用它:

void Main()
{
    string[][] query1 = {new [] {"this is a test","Yes", "This is a remark"},
                         new [] {"this is a test2","No", "This is the second remark"}};

    string[][] query2 = {new [] {"this is a test","",""},
                         new [] {"this is a test2","",""},
                         new [] {"this is a test3","",""},
                         new [] {"this is a test4","",""}};

    query1.Union(query2, new FirstElementComparer()).Dump();                         
}

Union使用EqualityComparer将query1中的元素与query2中的元素进行比较。它通过比较每个数组中的第一项来实现。

我将来请简化您的代码以显示实际问题的最小行数,理想情况是以完整的短程序的形式运行,而不是复制和粘贴任何帮助者将遇到麻烦的生产代码块用来重现问题。