Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq lambda表达式多对多表选择_C#_Sql_Linq_Lambda - Fatal编程技术网

C# Linq lambda表达式多对多表选择

C# Linq lambda表达式多对多表选择,c#,sql,linq,lambda,C#,Sql,Linq,Lambda,我有三张桌子,其中两张是多对多关系 图片: 这是中间mm表中的数据: 编辑: 直到这里,我得到了正确的4行返回,但它们都是相同的结果(我知道我需要4行返回,但有不同的结果) 我的问题是,我如何通过多对多表检索所有彩票,其中我只提供了LotteryOfferId 我想要实现的是通过LotteryDrawDateId从彩票表中获取数据 首先,我使用LotteryOfferId从中间表中获取DrawDate,然后通过中间表获得DrawDateId,以便在LotteryDrawDate表中使用它们。

我有三张桌子,其中两张是多对多关系

图片:

这是中间mm表中的数据:

编辑: 直到这里,我得到了正确的4行返回,但它们都是相同的结果(我知道我需要4行返回,但有不同的结果)

我的问题是,我如何通过多对多表检索所有彩票,其中我只提供了LotteryOfferId

我想要实现的是通过LotteryDrawDateId从彩票表中获取数据

首先,我使用LotteryOfferId从中间表中获取DrawDate,然后通过中间表获得DrawDateId,以便在LotteryDrawDate表中使用它们。从该表中,我需要在LotteryDrawDate表中按LotteryId检索Lottey表

我通过普通的SQL(LotteryOfferLotteryDrawDates是数据库中的中间表,在模型中看不到):

挑选 名称,Lotteries.CreatedBy,Lotteries.ModifiedOn,count(Lotteries.Id) 作为来自彩票的TotalDrawDates加入LotteryDrawDates on Lotteries.Id =LotteryDrawDates.LotteryId加入LotteryOfferLotteryDrawDates上的LotteryDrawDates.Id= LotteryOfferLotteryDrawDates.LotteryDrawDate\u Id 其中,LotteryOfferLotteryDrawDates.LotteryOffer\u Id=19 group by 名称,Lotteries.CreatedBy,Lotteries.ModifiedOn

但林克的情况不同:p

我想用lambda表达式来做这个。 谢谢

db.LotteryOffer.Where(lo=>lo.Id==)
.SelectMany(lo=>Lot.LotteryDrawDates)
.Select(ldd=>ldd.lotking)
.GroupBy(l=>new{l.Name,l.CreatedBy,l.ModifiedOn})
.选择(g=>new
{
g、 键。名称,
g、 Key.CreatedBy,
g、 基顿,
TotalDrawDates=g.Count()
} );
您可以执行以下操作:

var query = from lo in this._mediaBugEntityDB.LotteryOffers
            where lo.lotteryOfferId == lotteryOfferId
            from ld in lo.LotteryDrawDates
            group ld by ld.Lottery into grp
            select grp.Key;
我在查询语法中这样做,因为(在我看来)更容易看到发生了什么。主要的一点是通过
彩票
进行分组,因为您可以得到许多
彩票抽奖日期
,其中任何一个都可以有相同的
彩票

如果要显示每个
彩票的
LotteryDrawDates
计数,最好采用不同的方法:

from lot in this._mediaBugEntityDB.Lotteries.Include(x => x.LotteryDrawDates)
where lot.LotteryDrawDates
         .Any(ld => ld.LotteryDrawDates
                      .Any(lo => lo.lotteryOfferId == lotteryOfferId))
select lot

现在,您可以加载
lotkety
对象及其
LotteryDrawDates
集合,因此之后您可以访问
lotkety.LotteryDrawDates.Count()
而无延迟加载异常。

Woops我只提供了LotteryOfferId。我可能不得不选择嵌套Lambda表达式(如果可能)。谢谢你,汉克兄弟。这是可行的,现在我只是想弄清楚GroupBy是如何处理这个多对多语句的。你想按什么进行分组?正如你在我的SQL查询中看到的那样,我有
count(Lotteries.Id)
和按Lotteries.Name、Lotteries.CreatedOn、Lotteries.CreatedBy等进行分组。我用这个lambda一整天都在砸墙,我是认真的。为什么这么难。找不到任何好的lambda linq教程,一切都只是正常的linq查询。您好,谢谢您的帮助。我有这样的方法,但它说它不能转换匿名方法。如果我将
new lotket()
而不是匿名new,那么我不能声明
TotalDrawDates=g.Count()
。我如何在此查询中使用Count字段?这不起作用,因为我返回的是类型为的IQueryable。我想我现在明白你的意思了,请看我的编辑。
var query = from lo in this._mediaBugEntityDB.LotteryOffers
            where lo.lotteryOfferId == lotteryOfferId
            from ld in lo.LotteryDrawDates
            group ld by ld.Lottery into grp
            select grp.Key;
from lot in this._mediaBugEntityDB.Lotteries.Include(x => x.LotteryDrawDates)
where lot.LotteryDrawDates
         .Any(ld => ld.LotteryDrawDates
                      .Any(lo => lo.lotteryOfferId == lotteryOfferId))
select lot