1

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
4

4 回答 4

2

这样的事情呢?

var query = db.Customers.Select(c => new 
                {
                    CustId = c.CustId,
                    CustName = c.CustName,
                    Total_Orders = db.Orders.Where(o => o.CustId == c.CustId).Count()
                }
            );
于 2013-01-03T15:20:53.487 回答
1

请试试:

    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()
                };
于 2013-01-03T15:20:00.667 回答
0
var query = from c in context.Customers
            select new
            {
                c.CustId,
                c.CustName,
                Orders = c.Orders.Count(),
            };
于 2013-01-03T15:24:29.677 回答
0

以下是 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
于 2013-01-03T15:05:43.493 回答