I have a situation where I want to count the total transaction of the customer. I can do it in a mysql query but I don't know to do it in linq sql.
select c.name, count(r.custid)
from receipt r left join in cust c on (c.custid=r.custid)
group by r.custid
How can I convert it in linq sql?
I wouldn't start with the SQL... I'd start by thinking of what you're counting, which is basically the number of receipt records in the group which matches the customer. It sounds like you want something like:
var query = from customer in db.Customers
join receipt in db.Receipts
on customer.CustomerId equals receipt.CustomerId
into customerReceipts
select new { customer.Name, Count = customerReceipts.Count() };
Note that unlike many "left join" examples in LINQ, we don't use customerReceipts.DefaultIfEmpty()
because we only want the count - which should be 0 if there are no receipts for that customer.
See more on this question at Stackoverflow