LightSwitch Linq To Entities查询问题!

时间:2011-07-24 11:27:00

标签: sql linq entities visual-studio-lightswitch

我对Linq和实体框架很新。我正在尝试在我正在创建的LightSwitch订单管理应用程序中创建WCF RIA服务。我已经使用Linq创建了7或8个查询(有很多令人头疼的问题),但我成功了。现在我需要创建一个查询,通过使用三个表来按月获取TotalSales和TotalExpenses:

DetailsVentes(SaleDetails)包括:DateOfOrder,Quantity,UnitPrice,Total ....等 DetailsAchat(PurchaseDetails)由相同的字段组成 费用(费用)包括:DateOfExpense,ExpenseType,Cost .... etc

现在我想要一个查找每月净收入的查询(收入= TotalSales-TotalPurchases-TotalExpenses)

我试过了这个查询

      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 {.Month = 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))}

Vente是销售(订单)和DetailVentes是OrderDetails Achat是Purchase,以及DetailAchat PurchaseDetail 费用是一种费用。

问题是我得到了奇怪的结果。所有值(TotalVentes,TotalAchats,TotalCharges)都乘以13!

例如,如果我在2011年6月的总销售额为10 000美元,则此查询返回130 000美元!

我真的被困4天试图获得正确的查询。

我再说一遍,我是Linq的新手:p

非常感谢你。

编辑:我试过@Jason的建议我这样做了:

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}

结果仍然非常奇怪(非常高)!我可以遵循的任何例子吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

我终于通过将查询拆分为3个查询来实现它:

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

非常感谢Jason。

答案 1 :(得分:0)

我怀疑你想在查询中的某个地方加入,而不是笛卡尔积(没有任何连接条件的连接)。