1

I have this SQL query but I can not transfer it to linq query Entity Framework.

SELECT 
    RB.CustomerId AS ID, C.FullName AS 'FULL NAME', 
    C.PhoneNumber AS 'PHONE NUMBER', 
    COUNT(RB.CustomerId) AS BOOKS 
FROM  
    RentedBooks RB
JOIN 
    Customers C ON RB.CustomerId = C.Id
GROUP BY  
    C.FullName, C.PhoneNumber, RB.CustomerId;
1

1 Answer 1

2

The following code is equivalent of your query in linq:

var query = db.RentedBooks.GroupBy(x => new { x.Customer.FullName, x.CustomerId ,x.Customer.PhoneNumber })
.Select(x => new { ID = x.Key.CustomerId,
        x.Key.FullName,
        ,x.Key.PhoneNumber,Books = x.Count() });
Sign up to request clarification or add additional context in comments.

4 Comments

What does that do? How does it answer the question? Don't just blurt out code. Explain yourself! stackoverflow.com/help/how-to-answer
@RobThis is so obvious! The equivalent query in EF.
I would just expand Books = x.Count() for the book count. Prettifying the field names (i.e. "Full Name") can be done when displaying the data.
@StevePy Yes you are right, I've edited the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.