【问题标题】:C# LINQ to SQL Multiple entity joinC# LINQ to SQL 多实体连接
【发布时间】:2015-07-08 22:42:54
【问题描述】:

我有一个完整的表,其中包含跨年份、品牌和客户的数据,我想将其与客户主表连接起来。我希望每个输出记录是客户,一年的品牌 1 数量,一年的品牌 2 数量等以及品牌的总数。 我创建了一个类来接收输出。 当我查看统计表中的数据时,它的数据应该符合所使用的标准(即 debtorid、financialyearid 和 brandid。

我的选择目前看起来像这样

            var Debtors = Dbs.Debtors;
            var BikeSales = Dbs.SalesBikes.DefaultIfEmpty();
            if (sortStatePostCode)
            {
                dquery = from y in Debtors
                         from a in BikeSales where a.DebtorID == y.ID && a.FinYearID == finYear.ID && a.BikeBrandID == 1
                         from b in BikeSales where b.DebtorID == y.ID && b.FinYearID == finYear.ID && b.BikeBrandID == 2
                         from c in BikeSales where c.DebtorID == y.ID && c.FinYearID == finYear.ID && c.BikeBrandID == 3
                         from d in BikeSales where d.DebtorID == y.ID && d.FinYearID == finYear.ID && d.BikeBrandID == 4
                         from e in BikeSales where e.DebtorID == y.ID && e.FinYearID == finYear.ID && e.BikeBrandID == 7
                         from f in BikeSales where f.DebtorID == y.ID && f.FinYearID == finYear.ID && f.BikeBrandID == 8
                         from g in BikeSales where g.DebtorID == y.ID && g.FinYearID == finYear.ID && g.BikeBrandID == 9
                         from h in BikeSales where h.DebtorID == y.ID && h.FinYearID == finYear.ID && h.BikeBrandID == 10

                         orderby y.SortDelState == null ? "ZZZZ" : y.SortDelState, y.SortDelPCode == null ? "9999" : y.SortDelPCode, y.CustomerName
                         select new DealerBikeResult
                         {
                             LongYear = finyear,
                             CustomerCode = y.CustomerCode,
                             CustomerName = y.CustomerName,
                             City = y.SortDelCity,
                             PostCode = y.SortDelPCode,
                             State = y.SortDelState,
                             Terms = y.TermsCode,
                             Total = (
                                 (f == null ? 0 : (int)f.TotalQty) +
                                 (g == null ? 0 : (int)g.TotalQty) +
                                 (a == null ? 0 : (int)a.TotalQty) +
                                 (b == null ? 0 : (int)b.TotalQty) +
                                 (c == null ? 0 : (int)c.TotalQty) +
                                 (d == null ? 0 : (int)d.TotalQty) +
                                 (e == null ? 0 : (int)e.TotalQty) +
                                 (h == null ? 0 : (int)h.TotalQty)
                             ),
                             Bombtrack = f == null ? 0 : (int)f.TotalQty,
                             Fairdale = g == null ? 0 : (int)g.TotalQty,
                             Mirraco = a == null ? 0 : (int)a.TotalQty,
                             Radio = b == null ? 0 : (int)b.TotalQty,
                             Redline = c == null ? 0 : (int)c.TotalQty,
                             Sunday = d == null ? 0 : (int)d.TotalQty,
                             United = e == null ? 0 : (int)e.TotalQty,
                             WTP = h == null ? 0 : (int)h.TotalQty,
                             DealerBO = y.DealerBombTrack == null ? 3 : (int)y.DealerBombTrack,
                             DealerFA = y.DealerFairdale == null ? 3 : (int)y.DealerFairdale,
                             DealerMI = y.DealerMirraco == null ? 3 : (int)y.DealerMirraco,
                             DealerRA = y.DealerRadio == null ? 3 : (int)y.DealerRadio,
                             DealerRL = y.DealerRedline == null ? 3 : (int)y.DealerRedline,
                             DealerSU = y.DealerSunday == null ? 3 : (int)y.DealerSunday,
                             DealerUN = y.DealerUnited == null ? 3 : (int)y.DealerUnited,
                             DealerWP = y.DealerWTP == null ? 3 : (int)y.DealerWTP
                         };
            }

无论我向查询发送什么财务年度 ID,我的返回集都是空的。 当我在 SQL 管理工作室中创建一个视图时,我能够得到结果,它基本上实现了我想要得到的相同的东西,但我不想使用视图。

谁能指出我正确的方向。 (编辑代码以修正一些错别字)。

其他一些帖子以其他方式解决了这个问题(即您应该使用多选还是加入等)。我找不到合适的加入多列等于工作,我需要三列选择,这就是我尝试这种方式的原因。其他帖子已将这种方法显示为实现加入,而其他帖子则认为这不起作用。

Utilising Join ... On ... equals 格式不适用于我的多列,否则我会使用它。

【问题讨论】:

    标签: c# sql linq entity-framework


    【解决方案1】:

    除了您没有正确执行连接这一事实之外,查询 where 子句似乎也不好。不应该是这样的吗? -

    from a in Dbs.SalesBikes.DefaultIfEmpty() where a.DebtorID == y.ID && finYear.ID == a.FinYearID && a.BikeBrandID == 1
    from b in Dbs.SalesBikes.DefaultIfEmpty() where b.DebtorID == y.ID && b.FinYearID == finYear.ID && b.BikeBrandID == 2
    from c in Dbs.SalesBikes.DefaultIfEmpty() where c.DebtorID == y.ID && c.FinYearID == finYear.ID && c.BikeBrandID == 3
    from d in Dbs.SalesBikes.DefaultIfEmpty() where d.DebtorID == y.ID && d.FinYearID == finYear.ID && d.BikeBrandID == 4
    from e in Dbs.SalesBikes.DefaultIfEmpty() where  e.DebtorID == y.ID && e.FinYearID == finYear.ID && e.BikeBrandID == 7
    from f in Dbs.SalesBikes.DefaultIfEmpty() where  f.DebtorID == y.ID && f.FinYearID == finYear.ID && f.BikeBrandID == 8
    from g in Dbs.SalesBikes.DefaultIfEmpty() where  g.DebtorID == y.ID && g.FinYearID == finYear.ID && g.BikeBrandID == 9
    from h in Dbs.SalesBikes.DefaultIfEmpty() where  h.DebtorID == y.ID && h.FinYearID == finYear.ID && h.BikeBrandID == 10
    

    【讨论】:

    • 是的 Mahmudul,谢谢。我已编辑并重新发布。
    【解决方案2】:

    我相信我找到了来自 Jon Skeet 的帖子,它回答了我的问题。 显然我确实需要使用 Join On 语法,但我缺少的是需要模仿透明标识符,以便预处理器可以确保数据类型是相同的。

    如果成功的话,我现在就试试这个答案。

    再次感谢 Jon Skeet。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 2013-06-13
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    相关资源
    最近更新 更多