How to GroupBy data in Linq to SQL query - linq

I've got a problem in getting my groupby's right in a Linq to SQL query.
I have 3 tables: Customers, Orders and OrderItems. The OrderItems contain the quantity and price of the items purchased.
Now I simply would like to list all the customers with the total price of all orders.
I have (amongst a number of other tries) this, but it's not compiling. There is no TotalPrice item in the Intellisense where I have the ??? in the last line.
var q = (from c in db.Customers
join o in db.Orders on c.ID equals o.Customer
//join oi in db.OrderItems on o.ID equals oi.OrderID
group c by new {c.CustomerName, TotalPrice = o.OrderItems.Sum(x => x.Quantity * x.Price)} into groupby
select groupby);
foreach (var cust in q)
Console.WriteLine("{0} {1}", cust.Min(x => x.CustomerName), cust.Sum(x => x.???);
The (easy, understandable, straight forward and simple) SQL query will look like this:
SELECT C.ID, MIN(CustomerName) CustomerName, SUM(Quantity * Price) OrderPrice
FROM Customers C
INNER JOIN ORDERS O ON C.ID = O.Customer
INNER JOIN OrderItems OI ON O.ID = OI.OrderID
GROUP BY C.ID

This works for me:
This is sums by Customer and Order
var orders = (from c in context.Customers
join o in context.Orders on c.custid equals o.custid
join od in context.OrderDetails on o.orderid equals od.orderid
select new
{
c.custid,
o.orderid,
od.qty,
od.unitprice
}).GroupBy(c => new
{
c.custid,
c.orderid
}).Select(c => new
{
c.Key.custid,
c.Key.orderid,
Summ = c.Sum(d => d.qty * d.unitprice)
}).ToList();
This is sums by Customer only
var orders = (from c in context.Customers
join o in context.Orders on c.custid equals o.custid
join od in context.OrderDetails on o.orderid equals od.orderid
select new
{
c.custid,
od.qty,
od.unitprice
}
).GroupBy(c => new
{
c.custid,
}).Select(c => new
{
c.Key.custid,
Summ = c.Sum(d => d.qty * d.unitprice)
}).ToList();

I can mess with names, but here is LinQ expression with Extension methods syntax that match your SQL:
var q = db.Customers
.GroupBy(x => new
{
x.ID,
x.CustomerName,
}, (x, group) => new
{
Id = x.ID,
CustomerName = x.CustomerName,
TotalPrice = group.Select(y => y.Orders.Sum(z => z.OrderItems.Sum(a => a.Price * a.Quantity)))
});
foreach (var cust in q)
Console.WriteLine("{0} {1}", cust.CustomerName, cust.TotalPrice);

This is also working for me:
var q = (from c in db.Customers
join o in db.Orders on c.ID equals o.Customer
join oi in db.OrderItems on o.ID equals oi.OrderID
group new {c, o.ID, oi.Quantity, oi.Price} by new {c.ID, c.CustomerName} into grp
let TotalPrice = grp.Sum(x => x.Quantity * x.Price)
select new
{
CustomerID = grp.Key.ID,
CustomerName = grp.Key.CustomerName,
TotalPrice
});
and
var q = (from c in db.Customers
join o in db.Orders on c.ID equals o.Customer
join oi in db.OrderItems on o.ID equals oi.OrderID
group new { CustomerID = c.ID, CustomerName = c.CustomerName, Price = oi.Price, Quantity = oi.Quantity} by c.ID into grp
select new
{
ID = grp.Key,
CustomerName = grp.Min(x => x.CustomerName),
TotalPrice = grp.Sum(x => x.Price * x.Quantity)
});

Related

How do I convert this query into LINQ?

SELECT *
FROM
ProcedureLookup ProcLookup
INNER JOIN (SELECT PatientProcedures.PP_ProcedureId, COUNT(*) as ProcCount
FROM (PatientProcedures INNER JOIN Treatments ON PatientProcedures.PP_TreatmentId = Treatments.TS_TreatmentId)
WHERE YEAR(Treatments.TS_Date) = YEAR(GETDATE())
GROUP BY PatientProcedures.PP_ProcedureId) cyearProc ON ProcLookup.PL_ProcedureId = cyearProc.PP_ProcedureId
ORDER BY ProcCount DESC;
Here ProcedureLookup, Treatments, PatientProcedures are the tables.
Here linq query.
var result = (from ProcLookup in db.ProcedureLookup
join cyearProc in (
from p in db.PatientProcedures
join t in db.Treatments on p.PP_TreatmentId equals
t.TS_TreatmentId
where
t.TS_Date.Year == DateTime.Now.Year
group p by p.PP_ProcedureId into g
select new
{
PP_ProcedureId = g.Key,
ProcCount = g.Count()
}
) on ProcLookup.PL_ProcedureId equals
cyearProc.PP_ProcedureId
orderby cyearProc.ProcCount descending
select new
{
// Columns
PP_ProcedureId = ProcLookup.PP_ProcedureId,
ProcCount = cyearProc.ProcCount
}).ToList();

Linq left outer join and group by issue

Hi All my following query returns results like this:
lstView.DataSource = (from h in context.HolidayMains
join hd in context.HolidayDetails on h.Id equals hd.HolidayMainId into hd2
from hd in hd2.DefaultIfEmpty()
join e in context.Employees on h.CreatedBy equals e.Id into e2
from e in e2.DefaultIfEmpty()
join o in context.OfficeLocations on hd.OfficeLocation equals o.Id into o2
from o in o2.DefaultIfEmpty()
select new
{
h.HolidayTitle,
h.Date,
OfficeLocation = o.Name,
CreatedBy = e.Name,
h.Id
}).ToList();
But I need result like this :
How it can be done?

How to translate SQL to LINQ?

sql
select * from (select * from tabla limit 1) as a inner join table b on a.id = b.id where a.id = id
LINQ
from s in tableA
join c in (from a in tableB where a.id==id select a).FirstOrDefault() on s.id equals c.id
where s.id == id
select s
I want to translate this sql to linq but failed. How can I do translate?
I think this will do it, but you should check what sql is being generated by linq.
a = tableA.FirstOrDefault();
//First option
tableB.Where(xb => xb.id = a.id)
.Select(xb => new {a = a, b = xb})
.Where(abx => abx.a.id == id)
//Second option
tableB.Select(xb => new {a = a, b = xb})
.Where(abx => abx.a.id == id && abx.a.id = abx.b.id)

multiple tables join by Linq

I have 3 tables which named Player, PlayerDetails and Team. I want to write a linq statement such as
from p in Player join d in PlayerDetails on p.ID equals d.PID
and then right join team table, Player has a column named TID which face to Team table's ID.
I have tried to write a statement like
from p in Player join d in PlayerDetails on p.ID equals d.PID into PlayerGroup
from t in team join g in PlayerGroup on t.ID equals g.p.ID
It certainly can't work. I'm not sure how to write such type of query statement, table left join table2 then right join table3.
Who can help?
I believe you could do something like this:
var LeftJoin =
from p in Player
join d in PlayerDetails on p.ID equals d.PID into pd
from d in pd.DefaultIfEmpty()
select new
{
pID = p.ID,
pTID = p.TID,
dID = d.ID
};
var RightJoin =
from t in Team
join l in LeftJoin on t.ID equals l.pTID into tl
from l in tl.DefaultIfEmpty()
select new
{
tID = t.ID,
pID = l.pID,
pTID = l.PTID,
dID = l.dID
};
To do everything in one query, I think you could do (not tested) something like this:
var RightJoin =
from t in Team
join l in
(from p in Player
join d in PlayerDetails on p.ID equals d.PID into pd
from d in pd.DefaultIfEmpty()
select new
{
pID = p.ID,
pTID = p.TID,
dID = d.ID
})
on t.ID equals l.pTID into tl
from l in tl.DefaultIfEmpty()
select new
{
tID = t.ID,
pID = l.pID,
pTID = l.PTID,
dID = l.dID
};

How to Group more than one table By in LINQ

So, I understand that group by works basically like this:
var query = from c in context.Contacts
join o in context.Orders on c.Id on o.CustomerId
group c by o.Id
select new { g.Key, g.Sum(c => c.Whatever) };
Grouping like that only allows me access to the contents of c. But what if I wanted data from both tables c and o?
var query = from c in context.Contacts
join o in context.Orders on c.Id on o.CustomerId
//insert answer here
select new { g.Key, g.Sum(c => c.Whatever), g.Sum(o => o.Whatever) };
Is that even possible?
var query = from c in context.Contacts
join o in context.Orders on c.Id equals o.CustomerId
select new
{
Contact = c,
Order = o
} into ContactAndOrder
group ContactAndOrder by ContactAndOrder.Order.Id into g
select new
{
g.Key,
ContactWhatever = g.Sum(co => co.Contact.Whatever),
OrderWhatever = g.Sum(co => co.Order.Whatever)
};
Ben's answer is a bit over-complicated, IMO. It'd be simpler to reverse the select/join pairing like this:
var query = from o in context.Orders
join c in context.Contacts on o.CustomerId equals c.Id into Customers
group o by o.Id into g
select new {
g.Key,
ContactWhatever = g.Sum(o => o.Customers.Sum(c => c.Whatever)),
OrderWhatever = g.Sum(o => o.Whatever)
};

Resources