我是新Entity框架,我有一个问题,我一直在努力解决一段时间.基本上我有4个实体:用户,组,书和阅读列表.用户可以加入一个组,一组包含由阅读列表定义的书籍.我试图显示一个特定组的书籍列表,sql看起来像这样:
SELECT * FROM Books b WHERE b.Id IN ( SELECT BookID FROM ReadingList rl WHERE rl.GroupID = '3')
我通过从UserRepository查询当前用户来确定正在搜索的GroupID,目前“按组获取书籍”方法如下所示:
// Get books by group public IQueryable<Book> GetGroupBooks(string username) { UserRepository userRepository = new UserRepository(); int groupId = userRepository.GetUserGroupId(username); IQueryable<Book> q = from b in entities.Books where b.Id == 7 // temp - these values should be determined by // rl in entites.ReadingList select rl.BookID where r.GroupID == groupID select b; return q; }
显然这是一个临时措施,只能返回一本书,但我已经将其作为参考.这里的任何帮助或建议将不胜感激.
谢谢
解决方法
我没有测试过,但希望它会奏效.
entities.Books.Where( b => entities.ReadingList. Where(rl => rl.GroupId == groupId). Select(rl => rl.BookId). Contains(b.BookId) )