【问题标题】:LINQ Group By selecting with many-to-many association tableLINQ Group 通过选择多对多关联表
【发布时间】:2014-02-26 19:04:17
【问题描述】:

我有两个模型,PlansBookmarks,它们在 many-to-many relationship 中与数据库中的关联表相关联。具体来说,我正在研究Bookmark 与多个Plans 相关联的情况,如下所示...

BookmarkID | PlanID
    A      | 1
    A      | 2
    B      | 2

我想选择与特定PlanID没有关联的所有BookmarkIDs。所以如果PlanID = 1,我想选择B而不是A。

对于奖励积分,我可以轻松地获取 BookmarkID 结果并使用第二个 linq 查询获取所有书签,但是使用 select 函数或其他东西内联执行此操作会很酷。

【问题讨论】:

    标签: c# sql asp.net-mvc linq entity-framework


    【解决方案1】:

    我相信你想这样使用All

    int planId = 1;
    
    var query = from b in context.Bookmarks
                where b.Plans.All(p => p.PlanID != planId)
                select b.BookmarkID;
    

    【讨论】:

    • 这个是我最后用的,不知道是不是一样
    • var bookmarks = from b in _db.bookmarks group b by b.BookmarkKey into g where g.All(b => b.PlanKey != planKey) join b in _db.Bookmarks on g.Key等于 b.BookmarkKey 选择 b;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多