【发布时间】: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