我有三个表,其中两个是多对多关系。
Picture:
这是中间mm表中的数据:
Edit:到这里,我得到正确的 4 行返回,但它们都是相同的结果(我知道我需要返回 4 行,但有不同的结果)
return this._mediaBugEntityDB.LotteryOffers
.Find(lotteryOfferId).LotteryDrawDates
.Join(this._mediaBugEntityDB.Lotteries, ldd => ldd.LotteryId, lot => lot.Id, (ldd, lot) =>
new Lottery
{
Name = lot.Name,
CreatedBy = lot.CreatedBy,
ModifiedOn = lot.ModifiedOn
}).AsQueryable();
我的问题是,如何通过仅给出 LotteryOfferId 的多对多表检索所有彩票?
我想要实现的是通过LotteryDrawDateId从彩票表中获取数据。
首先,我使用 LotteryOfferId 从中间表获取 DrawDates,然后通过中间表获取 drawDateIds 以在 LotteryDrawDate 表中使用它们。我需要从该表中通过 LotteryDrawDate 表中的 LotteryId 检索 Lottey 表。
我通过普通 SQL 获得这个(LotteryOffersLotteryDrawDates 是数据库中的中间表,在模型中没有看到):
选择
名称、Lotteries.CreatedBy、Lotteries.ModifiedOn、计数(Lotteries.Id)
作为 Lotteries 中的 TotalDrawDates 加入 Lotteries.Id 上的 LotteryDrawDates
= LotteryDrawDates.LotteryId 加入 LotteryOffersLotteryDrawDates 上 LotteryDrawDates.Id =
LotteryOffersLotteryDrawDates.LotteryDrawDate_Id
其中 LotteryOffersLotteryDrawDates.LotteryOffer_Id = 19 分组依据
名称、Lotteries.CreatedBy、Lotteries.ModifiedOn
但 Linq 是不同的故事:P
我想用 lambda 表达式来做到这一点。
谢谢
db.LotteryOffer.Where(lo => lo.Id == <lotteryOfferId>)
.SelectMany(lo => lo.LotteryDrawDates)
.Select( ldd => ldd.Lottery )
.GroupBy( l => new { l.Name, l.CreatedBy, l.ModifiedOn } )
.Select( g => new
{
g.Key.Name,
g.Key.CreatedBy,
g.Key.ModifiedOn,
TotalDrawDates = g.Count()
} );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)