I am very new to LINQ and having great trouble grouping my tables so everything adds up together. I am trying to get this to display all the customers that have purchased more than $50 EVEN IF THEY HAVE MULTIPLE PURCHASES. in the SQL database using linq. Here is what I have so far, please help.
public class TopCustomerVM
{
public string Name { get; set; }
public decimal DollarsSold { get; set; }
public static List<TopCustomerVM> GetResults()
{
ACEEntities db = new ACEEntities();
return (from c in db.Customers
join o in db.Orders on c.CustomerId equals o.CustomerId
join l in db.OrderLines on o.OrderId equals l.OrderId
into x
select new TopCustomerVM {
Name = c.FirstName + c.LastName,
DollarsSold = x.Sum(asdf => asdf.UnitCost * asdf.Quantity)
})
.OrderByDescending(lkj => lkj.DollarsSold)
.Where(lkj => lkj.DollarsSold > 50)
.ToList();
}
}
I believe something like this will give you the desired output:
from record in
(from c in Customers
join o in Orders on c.Id equals o.CustomerId
join ol in OrderLines on o.Id equals ol.OrderId
let customerAndLine = new { Name = c.FirstName + ' ' + c.LastName, DollarsSold = ol.UnitCost * ol.Quantity }
group customerAndLine by customerAndLine.Name into grp
select new { Name = grp.Key, DollarsSold = grp.Sum(r => r.DollarsSold) })
where record.DollarsSold > 50
orderby record.DollarsSold descending
select record
Related
I want to display how many transfers did every customer.
I want to display CustomerId, Name and Totaly amount of transfers.
I mean I want to display CustomerId too, not only Name and Amount transfers.
Everything works fine except CustomerId: it is empty in my DataGridview.
I don't know how to do and I would really appreciate any kind of help.
Here is my code:
using (Db db = new Db())
{
var statistic = (from u in db.Transfers
join c in db.Customers on u.CustomerId equals c.CustomerId
where u.IsActive == true && u.Paid == true
group u by c.FirstName into g
select new
{
// here I get Headertext but not Id's value
Id = g.Select(x =>x.CustomerId),
Name = g.Key,
Totaly = g.Sum(x => x.Amount)
}).OrderByDescending(x => x.Totaly).ToList();
if (statistic != null)
{
dgvCustomerList.DataSource = statistic;
}
}
The correct way is to group it by CustomerId (because CustomerId is unique, you will get distinct customers inside select and then you can use FirstOrDefault() for getting FirstName property):
using (Db db = new Db())
{
var statistic = (from u in db.Transfers
join c in db.Customers on u.CustomerId equals c.CustomerId
where u.IsActive == true && u.Paid == true
group u by c.CustomerId into g
select new
{
Id = g.Key, // here I get Headertext but not Id's value
Name = g.FirstOrDefault().FirstName,
Totaly = g.Sum(x => x.Amount)
}).OrderByDescending(x => x.Totaly).ToList();
if (statistic != null)
{
dgvCustomerList.DataSource = statistic;
}
}
You can group by both customerId and FirstName
using (Db db = new Db())
{
var statistic = (from u in db.Transfers
join c in db.Customers on u.CustomerId equals c.CustomerId
where u.IsActive == true && u.Paid == true
group u by new {key c.FirstName,key c.CustumerId} into g
select new
{
Id = g.Key.CustumerId,//g.Select(x =>x.CustomerId), // here I get Headertext but not Id's value
Name = g.Key.FirstName,
Totaly = g.Sum(x => x.Amount)
}).OrderByDescending(x => x.Totaly).ToList();
if (statistic != null)
{
dgvCustomerList.DataSource = statistic;
}
}
I have 3 simple tables
tblQual (ID, QualName)
tblPerson (ID, PersonName)
tblPersonQual (ID, PersonID, QualID, ExpiryDate)
I would like to display a matrix with PersonName down the left, QualName at the top and ExpiryDate in the middle. How do I go about doing this?
I've tried this but no joy.
var QualMatrix = from c in db.tblPersonQual
join q in db.tblQual on c.QualID equals q.ID
join p in db.tblPerson on c.PersonID equals p.ID
group c by c.ID into g
select new
{
rowKey = g.Key,
rowData = g.Select(c => new { Qual = q.QualName, Expiry = c.Expiry })
};
In terms of output view something similar to this
As my understanding, you want to display a matrix with QualName, ExpiryDate and QualName. We can use the same query by just modifying group by clause.
var QualMatrix = from c in db.tblPersonQual
join q in db.tblQual on c.QualID equals q.ID
join p in db.tblPerson on c.PersonID equals p.ID
group new { q.QualName,p.PersonName} by new { c.ID,c.ExpiryDate } into g
select new
{
QualName=g.Select(e=>e.QualName).FirstOrDefault(),
ExpirtyDate=g.Key.Expiry,
PersonName=g.Select(e=>e.PersonName).FirstOrDefault(),
};
Hopefully, This will fulfil your requirement.
I'm trying to join two group by queries to get one results set.
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
// Query 1
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
// Query 2
select new PdPpAppCourseModel
{
courseID = paymentsCourseWise.Key.courseID,
cricosCode = paymentsCourseWise.Key.cricosCode,
courseName = paymentsCourseWise.Key.courseName,
totalAmount = totalRecievable.Sum(x => x.PP.totalAmount),
paidAmount = paymentsCourseWise.Sum(x => x.PD.paidAmount)
}).ToList();
Total about is taken from query 1 as it should group in payment plan(PP) level.
You can only combine enumerations of the same type, you could project both to a common class and then concatenate them:
var result1 = db1.table.Where(a=>a.value>0).Select( x=> new Foo() { //set props });
var result2 = db2.table.Where(a=>a.value>0).Select( x=> new Foo() { //set props });
var resultSum = result1.Concat(result2);
Similarly you can apply this in your code and join this two groups.
I am searching for equivalent to this query:
SELECT Prd.barcode , SumQUA = SUM(AType.unit*MIA.quantity)
FROM [dbo].[MerchantInventoryActivity] AS MIA
JOIN [dbo].[MerchantInventoryActivityType] AS AType ON (MIA.typeId = AType.id)
JOIN [dbo].[Product] AS Prd ON (MIA.productId = Prd.id)
WHERE MIA.invoiceId = '123'
GROUP BY Prd.barcode
i got stuck at the SumQUA = SUM(AType.unit*MIA.quantity) . Here's what I got so far:
(from mia in Entities.MerchantInventoryActivity
join Prd in Entities.Product on mia.productId equals Prd.id
join AType in Entities.MerchantInventoryActivityType on mia.typeId equals AType.id
where mia.invoiceId == invoiceId
group Prd by Prd.barcode into g
select new
{
Barcode = g.Key,
SumQUA = // ??
}).ToList();
Thank you!
You can project first to anonymous type and then group:
(from mia in Entities.MerchantInventoryActivity
join Prd in Entities.Product on mia.productId equals Prd.id
join AType in Entities.MerchantInventoryActivityType on mia.typeId equals AType.id
where mia.invoiceId == invoiceId
select new { Prd.barcode, AType.unit, MIA.quantity } into temp
group temp by temp.barcode into g
select new
{
Barcode = g.Key,
SumQUA = g.Sum(c => c.unit * c.quantity)
});
Add check for null when sum
(from mia in Entities.MerchantInventoryActivity
join Prd in Entities.Product on mia.productId equals Prd.id
join AType in Entities.MerchantInventoryActivityType on mia.typeId equals AType.id
where mia.invoiceId == invoiceId
select new { Prd.barcode, AType.unit, MIA.quantity } into temp
group temp by temp.barcode into g
select new
{
Barcode = g.Key,
SumQUA = g.Sum(c => c.unit??0 * c.quantity??0)
});
I'm trying to achieve the following SQL query which works the way I need it to into LINQ. I have 2 separate SQL queries which I join and than select the product if the groups price is greater than an individual price. I'm using LINQ to objects and have an Object Factory for the Purchases and Products table.
select DISTINCT(a.Name) from
(select p.Prod_ID, p.Name, SUM(p.Price) as Total
from Tb_AvailableProduct p, Tb_Purchases o
where p.Prod_ID = o.Prod_ID
group by p.Prod_ID, p.Name) a
JOIN
(select p.Prod_ID, p.Price
from Tb_AvailableProduct p, Tb_Purchases o
where p.Prod_ID = o.Prod_ID) b
on a.Prod_ID = b.Prod_ID
where a.Total > b.Price
I have this first query in Linq, but I only want to select a Product Name if that product's group price is greater than the product's individual price.. i.e more than one product has been sold. I'm trying to accomplish this with a sum and not using a count.
from o in this.myObjectFactory.ThePurchases
join p in this.myObjectFactory.TheProducts.Values
on o.ProductID equals p.ProductID
where o.CustomerID == customer.CustomerID
group p by p.ProductID into query1
select new { ProductID = query1.Key, TotalPurchasesThisYear = query1.Sum (p => p.Price)});
Something like this should probably work (it's very similar to your SQL query):
var result =
from a in
(
from p in TheProducts
join o in ThePurchases
on p.ProductID equals o.ProductID
group p by new { p.ProductID, p.Name, p.Price } into g
select new
{
ProductID = g.Key.ProductID,
Name = g.Key.Name,
Total = g.Sum(i => i.Price)
}
)
join b in
(
from p in TheProducts
join o in ThePurchases
on p.ProductID equals o.ProductID
select new
{
ProductID = p.ProductID,
Price = p.Price
}
)
on a.ProductID equals b.ProductID
where a.Total > b.Price
select a.Name;
result = result.Distinct();