【问题标题】:Linq lambda expression many to many table selectLinq lambda 表达式多对多表选择
【发布时间】:2013-10-19 14:58:58
【问题描述】:

我有三个表,其中两个是多对多关系。

图片:

这是中间mm表中的数据:

编辑: 到这里,我得到了正确的 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 是 DB 中的中间表,在模型中看不到):

选择 名称,Lotteries.CreatedBy,Lotteries.ModifiedOn,计数(Lotteries.Id) 来自 Lotteries 的 TotalDrawDates 加入 Lotteries.Id 上的 LotteryDrawDates = LotteryDrawDates.LotteryId 在 LotteryDrawDates.Id 上加入 LotteryOffersLotteryDrawDates = LotteryOffersLotteryDrawDates.LotteryDrawDate_Id 其中 LotteryOffersLotteryDrawDates.LotteryOffer_Id = 19 分组 名称、Lotteries.CreatedBy、Lotteries.ModifiedOn

但是 Linq 是不同的故事:P

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

【问题讨论】:

    标签: c# sql linq lambda


    【解决方案1】:
    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()
        } );
    

    【讨论】:

    • 糟糕,我只给了 LotteryOfferId。我可能不得不嵌套选择。 = Lambda 表达式(如果可能)。谢谢
    • 谢谢兄弟。这行得通,现在我只是想弄清楚 GroupBy 如何处理这个多对多语句。
    • 你想按什么分组?
    • 正如您在我的 SQL 查询中看到的那样,我有 count(Lotteries.Id) 并按 Lotteries.Name、Lotteries.CreatedOn、Lotteries.CreatedBy 等分组。我整天都在用这个 lambda 敲墙,我我是认真的。为什么这么难。找不到什么好的 lambda linq 教程,一切都只是普通的 linq 查询。
    • 您好,感谢您的额外帮助。我有它,但它说它不能转换匿名方法。 pastebin.com/ZvUb8TSE 如果我放 new Lottery() 而不是匿名 new,那么我不能声明 TotalDrawDates = g.Count()
    【解决方案2】:

    你可以这样做:

    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;
    

    我在查询语法中这样做,因为(在我看来)更容易看到发生了什么。重点是按Lottery 分组,因为您会得到多个LotteryDrawDates,其中任何一个都可以有相同的Lottery

    如果您想显示每个 LotteryLotteryDrawDates 的计数,最好采用不同的方法:

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

    现在您获得了已加载 LotteryDrawDates 集合的 Lottery 对象,因此之后您可以访问 lottery.LotteryDrawDates.Count() 而不会出现延迟加载异常。

    【讨论】:

    • 我如何在这个查询中使用计数字段?
    • 这不起作用,因为我返回 IQueryable 类型。 IQueryable
    • 我想我现在明白你的意思了,请看我的编辑。
    猜你喜欢
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2020-06-21
    • 2019-07-06
    相关资源
    最近更新 更多