I'm trying to write a linq query which should sum order_detail lines from the Northwind.mdb and then return a summarized total along with a few details of the employee responsible. I'm using LINQpad to test it and this is what I have so far
void Main()
{
var result = (from e in Employees
join o in Orders on e.EmployeeID equals o.EmployeeID
join d in OrderDetails on o.OrderID equals d.OrderID
where o.OrderID == 10250
group new { e, o, d } by new
{
o.OrderID, e.Address, e.BirthDate, e.City, e.Country,
e.LastName, e.FirstName, e.Title, e.TitleOfCourtesy, e.HireDate,
e.Region, e.PostalCode, e.HomePhone,
e.Extension, e.ReportsTo,e.PhotoPath, e.EmployeeID,
d.Quantity, d.UnitPrice, d.Discount
}
into grp select new
{
Name = grp.Key.FirstName + " " + grp.Key.LastName,
OrderID = grp.Key.OrderID,
Address = grp.Key.Address,
SalesTotal = grp.Sum(x => x.d.UnitPrice * x.d.Quantity)
});
result.Dump();
}
Output from LINQPad
I was expecting just one line with a total of 1813.00. Can someone tell me what I'm doing wrong?
You're not grouping at the right level. If you change it to group by the employee, then you should get the right results:
void Main()
{
var result = (from e in Employees
join o in Orders on e.EmployeeID equals o.EmployeeID
join d in OrderDetails on o.OrderID equals d.OrderID
where o.OrderID == 10250
group new { e, o, d } by new
{
e.EmployeeID, e.FirstName, e.LastName, e.Address
}
into grp select new
{
Name = grp.Key.FirstName + " " + grp.Key.LastName,
Address = grp.Key.Address,
SalesTotal = grp.Sum(x => x.d.UnitPrice * x.d.Quantity)
});
result.Dump();
}
If you want one line per Order, group by that too:
void Main()
{
var result = (from e in Employees
join o in Orders on e.EmployeeID equals o.EmployeeID
join d in OrderDetails on o.OrderID equals d.OrderID
where o.OrderID == 10250
group new { e, o, d } by new
{
e.EmployeeID, e.FirstName, e.LastName, e.Address,
o.OrderID
}
into grp select new
{
Name = grp.Key.FirstName + " " + grp.Key.LastName,
OrderID = grp.Key.OrderID,
Address = grp.Key.Address,
SalesTotal = grp.Sum(x => x.d.UnitPrice * x.d.Quantity)
});
result.Dump();
}
Related
Can someone tell me how to write an EF query that equals to this:
SELECT OI.orderid,
OI.inventoryid,
VP.vendorid
FROM orders O
INNER JOIN orderitems OI
ON O.orderid = OI.orderid
INNER JOIN vendorparts VP
ON VP.inventoryid = OI.inventoryid
WHERE (SELECT Count(*)
FROM vendorparts
INNER JOIN vendors
ON vendorparts.vendorid = vendors.vendorid
AND vendors.candropship = 1
WHERE vendorparts.inventoryid = VP.inventoryid
AND vendorparts.vendorid IN ( 1, 17 )) > 1
I'm trying to do something like this...
var q = from o in _context.Orders
join oi in _context.Orderitems on o.Orderid equals oi.Orderid
join vp in _context.Vendorparts on oi.Inventoryid equals vp.Inventoryid
where (from vp2 in _context.Vendorparts
where vp2.Inventoryid == vp.Inventoryid
select count(*)) > 1
but obviously that is syntatctically not allowed.
Long answer:
var vendorIds = new int[] { 1, 17 };
_context
.Orders
.Join(_context.Orderitems,
o => o.Orderid,
oi => oi.Orderid,
(o, oi) => new { o, oi })
.Join(_context.Vendorparts,
o_oi => o_oi.oi.Inventoryid,
vp => vp.Inventoryid,
(o_oi, vp) => new { o_oi.o, o_oi.oi, vp })
.Where(o_oi_vp => _context.Vendorparts
.Where(vp
=> vp.Inventoryid == o_oi_vp.vp.Inventoryid
&& vendorIds.Contains(vp.Vendorid))
.Join(_context.Vendors.Where(v => v.Candropship),
vp => vp.Vendorid,
v => v.Vendorid,
(vp, v) => new { vp, v })
.Any())
.Select(o_oi_vp => new {
o_oi_vp.oi.orderid,
o_oi_vp.oi.inventoryid,
o_oi_vp.vp.vendorid
});
Short answer: use a stored procedure.
Here I want to join the out put of the first query(one column) to the result of the 2nd query to get a one result set. How can I merge them.(CONCAT doesn't work as required. eg: var query2 = query.concat(query1);)
var query = (from PP in _db.paymentPlans
join APP in _db.Applications on PP.applicationID equals APP.ApplicationId
join C in _db.Courses on APP.courseID equals C.courseID
where PP.active == true && APP.agentID == agentID
orderby C.courseID ascending
group new {C,PP} by new {C.courseID} into totalRecievable
select new PdPpAppCourseModel
{
courseID = totalRecievable.Key.courseID,
totalAmount = totalRecievable.Sum(x => x.PP.totalAmount)
}).ToList();
var query1=(from PD in _db.paymentDetails
join PP in _db.paymentPlans on PD.paymentPlanID equals PP.paymentPlanID
join APP in _db.Applications on PP.applicationID equals APP.ApplicationId
join C in _db.Courses on APP.courseID equals C.courseID
where PP.active == true && APP.agentID == agentID
orderby C.courseID ascending
group new { C,PD } by new { C.courseID, C.cricosCode, C.courseName } into paymentsCourseWise
select new PdPpAppCourseModel
{
courseID = paymentsCourseWise.Key.courseID,
cricosCode = paymentsCourseWise.Key.cricosCode,
courseName = paymentsCourseWise.Key.courseName,
paidAmount = paymentsCourseWise.Sum(x => x.PD.paidAmount)
}).ToList();
You could join query1 and query like this
var result = (from q1 in query1
join q in query on q1.courseID = q.courseID
select new PdPpAppCourseModel
{
courseID = q1.Key.courseID,
cricosCode = q1.Key.cricosCode,
courseName = q1.Key.courseName,
paidAmount = q1.Sum(x => x.PD.paidAmount),
totalAmount = q.totalAmount
}).ToList();
I try to translate this SQL code :
SELECT w.Id, w.LastName, w.FirstName, SUM(d.Price*dt.Number) AS somme
FROM Waiter w
INNER JOIN Client c on w.Id = c.WaiterId
INNER JOIN DisheOnTable dt on c.Id = dt.ClientId
INNER JOIN Dishe d on dt.DisheId = d.Id
GROUP BY w.Id, w.LastName, w.FirstName
ORDER BY somme DESC;
in entity framework.
I tried something like this
var query2 = (from w in db.Waiter
join c in db.Client on w.Id equals c.WaiterId
join dt in db.DisheOnTable on c.Id equals dt.ClientId
join d in db.Dishe on dt.DisheId equals d.Id
group w by new { w.Id, w.LastName, w.FirstName } into g
//orderby g.Select() descending
select new
{
id = g.Key.Id,
lastname = g.Key.LastName,
firstname = g.Key.FirstName,
total = g.Sum(q => q.)
});
but my sum doesn't work (after multiple research and try) and i don't know how to multiply my variables.
PS : The SQL statement works well, i tried it.
Thank you for helping guys ! :)
You need to group on both dish and DishOnTable alias as Price is in Dish and Number is in DishOnTable:
group new{ d,dt} by new {w.Id, w.LastName, w.FirstName} into g
and now sum the columns which you want from it
select new {
id = g.Key.Id,
lastname = g.Key.LastName,
firstname = g.Key.FirstName,
total = g.Sum(q => q.d.Price * q.dt.Number)
}).OrderBy(x=>x.total)
How do I change this query to an outer join so that customers with no orders still appear in the results?
Customers
.Join(
Orders,
c => c.ID,
r => r.ID,
(c, r) =>
new
{
Name = c.Name,
Address = c.Address,
OrderNumber = r.OrderNumber,
OrderDetails = r.OrderDetails
}
).ToList()
from c in context.Customers
join o in context.order on new { cid = c.cid } equals new { cid = o.cid } into ljoin
from l in ljoin.DefaultIfEmpty()
select new()
{
//whatever
};
How to convert this one to LINQ?
select d.UserID, d.Content, d.UpdateTime
from DiaryPosts as d
where d.UserID = 1
/* Friends posts */
Union
select d.UserID, d.Content, d.UpdateTime
from DiaryPosts as d
join Friends as fr
on d.UserID = fr.FriendID
where fr.UserID = 1
/* My followings */
Union
select d.UserID, d.Content, d.UpdateTime
from DiaryPosts as d
join Followers as fl
on d.UserID = fl.FollowerID
where fl.UserID = 1
/* order by UpdateTime desc */
order by 3 desc
I tried this:
var diaryPosts = (from d in db.DiaryPosts
join e in db.EstadosDeAlma
on d.EstadosDeAlmaID equals e.ID
join u in db.User
on d.UserID equals u.ID
join fr in db.Friends
on d.UserID equals fr.FriendID
where fr.UserID == userset.ID
join fl in db.Followers
on d.UserID equals fl.UserID
where fl.FollowerID == userset.ID
orderby d.ID descending
select new DiaryPostsSet
{
PostID = d.ID,
EstadoDeAlmaID = e.ID,
EstadoDeAlma = e.Title,
Author = u.Nickname,
Thumbnail = u.Thumbnail,
AuthorComment = d.Content,
UserID = u.ID,
IsDuplicated = d.IsDuplicated,
FriendID = d.FriendID,
FriendName = u.Nickname,
Time = d.UpdateTime,
MessagesCount = d.FriendMessages.Count(m => m.DiaryPostsID == d.ID)
}).Take(6).ToList();
It doesn't show me any result. I tried with EF clauses but when I have Union, I don't know how to perform the next Join.
Could anyone help me please?
There is a Union Method in LINQ
Here is a hint. When things get difficult break them down, then simplify. I will help you break down the problem and leave the simplification to you.
var query1 = (from d in db.DiaryPosts
where d.UserID = 1
select new {
UserID = d.UserID
Content = d.Content
UpdateTime = d.UpdateTime
}).ToList();
var query2 = (from d in db.DiaryPosts
join f in db.Friends
on d.UserId = f.FriendId
where f.UserId = 1
select new {
UserID = d.UserID
Content = d.Content
UpdateTime = d.UpdateTime
}).ToList();
var query3 = (from d in db.DiaryPosts
join f in db.Followers
on d.UserId = f.FollowerID
where f.UserId = 1
select new {
UserID = d.UserID
Content = d.Content
UpdateTime = d.UpdateTime
}).ToList();
var myunionQuery = query1.Union(query2).Union(query3).OrderBy(d => d.UpdateTime);