c# LINQ到SQL的多实体连接

本文关键字:实体 连接 SQL LINQ | 更新日期: 2023-09-27 18:01:37

我有一个表,其中包含跨年份、品牌和客户的数据,我想将其与客户主表连接。我希望每个输出记录都是customer, brand1的年度数量,brand2的年度数量等,以及品牌的总数。我创建了一个类来接收输出。当我查看统计表中的数据时,它有数据应该符合使用的标准(即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
                         };
            }

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

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

其他几个帖子以其他方式解决了这个问题(例如,你应该使用多选还是Join On等)。我找不到一个合适的加入与多列等于工作,我需要三列选择,这就是为什么我已经尝试过这种方式。其他帖子显示这种方法作为实现连接,而其他人认为这不能工作。

using Join…在…= =格式对我来说不适合多列,否则我会使用它

c# LINQ到SQL的多实体连接

除了您没有正确执行连接之外,查询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

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

我现在就试一下,如果成功的话,就把这个作为答案。

再次向Jon Skeet致敬