Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework subquery

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 and readingLists. A user can join a group and a group contains books - defined by readingList. I am trying to display a list of books for a specific group, the SQL looks like this:

SELECT * FROM Books b
WHERE b.Id IN (
    SELECT BookID FROM ReadingList rl
        WHERE rl.GroupID = '3')

I determine the GroupID being searched by querying the current user from a UserRepository and currently the 'get books by group' method is looking like this:

// 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;
}

Obviously this is a temporary measure and only returns one book, but I have included it for reference. Any help or advice here would be much appreciated.

Thanks

like image 236
Apollo Avatar asked Mar 26 '11 14:03

Apollo


People also ask

What is the difference between CTE and subquery?

CTEs can be recursive: A CTE can be used recursively, which a subquery cannot. This makes it especially well suited to tree structures, in which information in a given row is based on the information from the previous row(s). The recursion feature can be implemented with RECURSIVE and UNION ALL .

Which is faster subquery or correlated subquery?

A correlated subquery is much slower than a non-correlated subquery because in the former, the inner query executes for each row of the outer query. This means if your table has n rows then whole processing will take the n * n = n^2 time, as compared to 2n times taken by a non-correlated subquery.

What is difference between subquery and correlated subquery?

Subqueries can be categorized into two types: A noncorrelated (simple) subquery obtains its results independently of its containing (outer) statement. A correlated subquery requires values from its outer query in order to execute.


2 Answers

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.

like image 162
Nix Avatar answered Sep 28 '22 04:09

Nix


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)
)
like image 43
Mayank Avatar answered Sep 28 '22 03:09

Mayank