简体   繁体   English

C#LINQ to SQL多个实体联接

[英]C# LINQ to SQL Multiple entity join

I have a table full of data split across year, brand and customer which I want to join with a customer master table. 我有一个表,其中包含我想与客户主表合并的跨年度,品牌和客户的数据。 I want each output record to be customer, brand1 qty for the year, brand2 qty for the year, etc and a total of the brands. 我希望每个输出记录都是客户,一年中的brand1数量,一年中的brand2数量等,以及所有品牌。 I created a class to recieve the output. 我创建了一个类来接收输出。 When I view the data in the statistics table it has data which should meet the criteria used (ie debtorid, financialyearid and brandid. 当我在统计信息表中查看数据时,其数据应符合所使用的标准(即,债务人,财务年度和品牌)。

My select currently looks like this 我的选择当前看起来像这样

            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
                         };
            }

No matter what financialyearid I send to the query my return set is empty. 无论我向查询发送什么财务年度ID,我的返回集都是空的。 I am able to get results when I create a view in SQL management studio which essentially achieves the same thing I want to get but I dont want to use views. 在SQL Management Studio中创建视图时,我能够获得结果,该视图实质上实现了我想要获得的相同功能,但我不想使用视图。

Could anyone point me in the right direction please. 任何人都可以指出我正确的方向。 (Edited code to fix a few typos). (修改后的代码可以解决一些输入错误)。

Several other posts have addressed the question in other ways (ie should you use multi-select or Join On etc.). 其他几篇文章也以其他方式解决了这个问题(例如,您应该使用多选功能还是Join On等)。 I could not find a suitable Join On with multi column equals to work from and I need the three column selection which is why I have tried this way. 我找不到适合的多列等于加入工作,并且需要三列选择,这就是为什么我尝试了这种方式。 Other posts have shown this method as achieving a join while others suggest this can't work. 其他帖子将这种方法显示为实现了连接,而其他帖子则表明此方法无效。

Utilising Join ... On ... equals format has not worked for me with multiple columns or I would be using it. 使用Join ... On ... equals格式不适用于多列,否则我将使用它。

Apart from the fact that you are not doing joins properly, that query where clause does not seems okay. 除了您没有正确执行联接的事实之外,该查询where子句似乎还不行。 Wasn't it supposed to be something like this ? 难道不应该是这样吗? - -

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

I believe I found a post from one Jon Skeet which answers my question. 我相信我从一个Jon Skeet找到了一个帖子,回答了我的问题。 Apparently I do need to use Join On syntax but what I was missing was the need to imitate transparent identifiers so that the preprocessor can be sure that datatypes are the same. 显然我确实需要使用Join On语法,但是我缺少的是需要模仿透明标识符,以便预处理器可以确保数据类型相同。

I shall try that now and make this the answer if it suceeds. 我现在尝试一下,如果可以的话,使这个答案成为可能。

Again, kudos to Jon Skeet. 同样,对乔恩·斯基特(Jon Skeet)表示敬意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM