Below is my SQL Statement, I am Unable to Convert to LINQ
select o.CustId,c.CustName,Count(o.CustId) as Total_Orders from Customer c
inner join Orders o on c.CustId = o.CustId group by o.CustId,c.CustName
Below is my SQL Statement, I am Unable to Convert to LINQ
select o.CustId,c.CustName,Count(o.CustId) as Total_Orders from Customer c
inner join Orders o on c.CustId = o.CustId group by o.CustId,c.CustName
这样的事情呢?
var query = db.Customers.Select(c => new
{
CustId = c.CustId,
CustName = c.CustName,
Total_Orders = db.Orders.Where(o => o.CustId == c.CustId).Count()
}
);
请试试:
var query = from c in Customer
join o in Orders
on c.CustId
equals o.CustId
group x by new {o.CustId, c.CustName}
into g
select new
{
g.Key.CustId,
g.Key.CustName,
Total_Orders = g.Count()
};
var query = from c in context.Customers
select new
{
c.CustId,
c.CustName,
Orders = c.Orders.Count(),
};
以下是 LINQ 查询(db
是 EntityFramework DBContext)。
var query = from c in db.Customers
join o in db.Orders on c.CustId equals o.CustId into g
groupby new { CustID = c.CustId, CustName=c.CustName } into gs
select new { CustID = gs.Key.CustId, CustName=gs.Key.CustName, Count= gs.Count() } into gs