0

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;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

1

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() });
Saman Gholami
  • 3,416
  • 7
  • 30
  • 71