简体   繁体   English

在实体框架中通过SQL GROUP BY语句执行GROUP BY查询

[英]Performing a GROUP BY query in Entity Framework from a SQL GROUP BY statement

I am writing an invoicing system in VB.NET and ASP.NET using Entity Framework to store my objects. 我正在使用Entity Framework在VB.NET和ASP.NET中编写发票系统,以存储我的对象。 I want to sum up all of the billable line items for any specific month and have them collated into an invoice. 我想汇总任何特定月份的所有可结算订单项,然后将它们整理成发票。 The following SQL query accomplishes this, however I can't get it working in Entity Framework code-first: 以下SQL查询可完成此操作,但是我无法使其在Entity Framework代码优先的情况下起作用:

SQL query: SQL查询:

SELECT DISTINCT 
    [database].[dbo].[WorkOrderDetails].PriceCodeID,
    [database].[dbo].[WorkOrderDetails].[Description],
    SUM([database].[dbo].[WorkOrderDetails].[Quantity]) AS [Quantity],
    SUM([Subtotal]) AS Subtotal,
    SUM([Total]) AS Total
FROM 
    [database].[dbo].[WorkOrderDetails] 
INNER JOIN 
    [database].[dbo].[WorkOrders] ON [database].[dbo].[WorkOrderDetails].[WorkOrderID] = [database].[dbo].[WorkOrders].[WorkOrderID]
WHERE 
    [ClientID] = 182 
    AND [WorkOrders].[Date] >= '10/1/2016' 
    AND [WorkOrders].[Date] < '10/31/2016' 
GROUP BY 
    [PriceCodeID], [Description]

The output of this query is similar to the following: 该查询的输出类似于以下内容:

PriceCodeID | Description | Quantity | Subtotal | Total  
------------+-------------+----------+----------+--------
26            BOX REFILE        19         47.50    47.50
28            BOX RETRIEVAL     15         37.50    37.50
98            Del/Pu Out       376        545.20   545.20
95            Shredding      16893        760.19   760.19

My objects are: 我的对象是:

  • WorkOrder : WorkOrderID, Date, ClientID (foreign key) , List (of WorkOrderDetail ) WorkOrder :WorkOrderID,日期,ClientID (外键) ,列表( WorkOrderDetail

  • WorkOrderDetail : WorkOrderDetailID, PriceCodeID (foreign key) , Description, UnitPrice, Quantity, Subtotal, Tax, Total, WorkOrderID (foreign key) WorkOrderDetail :WorkOrderDetailID,PriceCodeID (外键) ,描述,单价,数量,小计,税金,总计,WorkOrderID (外键)

  • PriceCode : PriceCodeID, ShortCode, Description, UnitPrice, Tax PriceCode :PriceCodeID,ShortCode,描述,单价,税金

I'm attempting to use the statement below, but I'm unsure how to use the GroupBy statement. 我正在尝试使用下面的语句,但是不确定如何使用GroupBy语句。

newInvoice.Details = _db.WorkOrderDetails
                        .Include("WorkOrder")
                        .Where(Function(wod) wod.WorkOrder.Date >= newInvoice.StartDate And 
                                             wod.WorkOrder.Date < newInvoice.EndDate And 
                                             wod.WorkOrder.ClientID = newInvoice.ClientID)
                        .GroupBy(...)
                        .ToList()

You can get the aggregated results by 您可以通过以下方式获得汇总结果

Dim aggregate = From wod in _db.WorkOrderDetails
                Where wod.WorkOrder.Date >= newInvoice.StartDate And 
                      wod.WorkOrder.Date < newInvoice.EndDate And 
                      wod.WorkOrder.ClientID = newInvoice.ClientID)
                Group wod By Key = New With { Key wod.PriceCodeID, Key Description }
                Into Group
                Select agg = Key.PriceCodeID,
                             Key.Description,
                             Quantity = Group.Sum(Function(wod) wod.Quantity),
                             Subtotal = Group.Sum(Function(wod) wod.Subtotal),
                             Total = Group.Sum(Function(wod) wod.Total)

You can't directly create new WorkOrderDetail in this query, because EF doesn't allow creating new entity types in a LINQ query. 您不能在此查询中直接创建新的WorkOrderDetail ,因为EF不允许在LINQ查询中创建新的实体类型。 But you can use aggregate.AsEnumerable() to continue: 但是您可以使用aggregate.AsEnumerable()继续:

newInvoice.Details = aggregate.AsEnumerable()
                     .Select(Function (wod) New WorkOrderDetail ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM