Entity Framework subquery

后端 未结 2 1904
一整个雨季
一整个雨季 2020-12-31 12:35

Guys I am new to Entity Framework and I\'m having a bt of a problem that I have been trying to solve for quite a while. Basically I have 4 entities: users, groups, books an

相关标签:
2条回答
  • 2020-12-31 12:55

    I haven't tested it but hopefully it will work.

    entities.Books.Where(
    b => entities.ReadingList.
    Where(rl => rl.GroupId == groupId).
    Select(rl => rl.BookId).
    Contains(b.BookId)
    )
    
    0 讨论(0)
  • 2020-12-31 13:11

    Personally I think there is a better solution(untested of course):

    First select from ReadList by GroupdID, then join in books on BookID.

    IQueryable<Book> q = 
           from rl in entities.ReadingList
           join b in entities.Books on rl.BookID equals b.BookID
           where rl.GroupdID ==groupID
           select b;
    
    var books = q.ToList()
    

    Please let me know if you have any issues.

    0 讨论(0)
提交回复
热议问题