LightSwitch Linq To Entities query problem !
I'm very new to Linq and Entities Framework. I'm trying to create a WCF RIA Service into a LightSwitch Orders Management application I'm creating. I've already created 7 or 8 queries using Linq (with lot of headaches) but I succeded. Now I need to create a query who gets TotalSales and TotalExpenses by month by using three tables :
DetailsVentes (SaleDetails) composed of : DateOfOrder, Quantity, UnitPrice, Total.... etc DetailsAchat (PurchaseDetails) composed of the same fields Charges (Expenses) composed of : DateOfExpense, ExpenseType, Cost....etc
Now I want to have a query that finds the Net Income per month (income=TotalSales-TotalPurchases-TotalExpenses)
I tried this query
Return From od In Me.Context.DetailsVentes
From od2 In Me.Context.DetailsAchats
From od3 In Me.Context.Charges
Group By Month = od.Vente.DateCommande.Month, Year = od.Vente.DateCommande.Year
Into g = Group
Select New TCR With {.Mo开发者_如何学JAVAnth = Month, .Year = Year,
.MonthYearString = Month & "/" & Year,
.TotalVentes = g.Sum(Function(s) (s.od.PrixUnitaire * s.od.Quantité)),
.TotalAchats = g.Sum(Function(s) (s.od2.PrixAchat * s.od2.Quantité)),
.TotalCharges = g.Sum(Function(s) (s.od3.Cout))}
Where Vente is Sale (Order) and DetailVentes is OrderDetails Achat is Purchase, and DetailAchat PurchaseDetail a Charge is an Expense.
The Problem is I got weird results. All values (TotalVentes, TotalAchats, TotalCharges) are multiplicated by 13 !
For eg if I have 10 000 USD totalSales for June 2011, this query returns 130 000 USD !
I'm really stuck 4 days trying to get the correct query.
I repeat I'm new to Linq :p
Thank you very much.
Edit : I tried @Jason advice I did this :
Return From c In Me.Context.Charges
Join v In Me.Context.DetailsVentes On c.DateCharge.Month Equals v.Vente.DateCommande.Month And c.DateCharge.Year Equals v.Vente.DateCommande.Year
Join a In Me.Context.DetailsAchats On c.DateCharge.Month Equals a.Achat.DateCommande.Month And c.DateCharge.Year Equals a.Achat.DateCommande.Year
Group By month = c.DateCharge.Month, Year = c.DateCharge.Year
Into g = Group
Select New TCR With {.Month = month, .Year = Year, .MonthYearString = "", .TotalAchats = g.Sum(Function(c) (c.a.Quantité * c.a.PrixAchat)), .TotalCharges = 45000, .TotalVentes = 250000, .TCRSingle = 0}
And results are still very weird (very high) ! Any exemple I can follow ??
Thank you.
I finally had it works by splitting the query to 3 queries like this :
Dim vt = From od In Me.Context.DetailsVentes
Group By Month = od.Vente.DateCommande.Month, Year = od.Vente.DateCommande.Year
Into g = Group
Select New VentesParMois With {.Month = Month, .Year = Year,
.TotalVentesSingle = g.Sum(Function(od) _
(od.PrixUnitaire * od.Quantité))}
Dim at = From od In Me.Context.DetailsAchats
Group By Month = od.Achat.DateCommande.Month, Year = od.Achat.DateCommande.Year
Into g = Group
Select New AchatsParMois With {.Month = Month, .Year = Year,
.TotalAchatsSingle = g.Sum(Function(od) _
(od.PrixAchat * od.Quantité))}
Dim ct = From od In Me.Context.Charges
Group By Month = od.DateCharge.Month, Year = od.DateCharge.Year
Into g = Group
Select New ChargesParMois With {.Month = Month, .Year = Year,
.TotalChargesSingle = g.Sum(Function(od) _
(od.Montant))}
Dim q = From a In at.AsEnumerable, v In vt.AsEnumerable, c In ct.AsEnumerable
Where a.MonthYear = v.MonthYear AndAlso a.MonthYear = c.MonthYear
Select New TCR With {.Month = a.Month, .Year = a.Year, .TotalAchats = a.TotalAchats, .TotalVentes = v.TotalVentes, .TotalCharges = c.TotalCharges}
Return q.AsQueryable
Thank you very much Jason.
I suspect you want a join somewhere in your query rather than a cartesian product (a join without any join conditions).
精彩评论