简体   繁体   English

C#/ LINQ:当列的值不恒定时如何分组

[英]C# / LINQ: How to GroupBy when a column's value is not constant

The snippet below shows the method I am using to (try to) pick the "latest" driver of a vehicle when they have the same VIN. 下面的代码片段显示了当他们具有相同的VIN时,我用来(尝试)选择“最新”车辆驾驶员的方法。 This method is called AFTER I use LINQ to get all transactions for all vehicles at a certain location. 在我使用LINQ在特定位置获取所有车辆的所有交易之后,此方法称为。 In the 在里面

group vehicle by new
section, DriverLastName is the only column that can change from row to row. 部分, DriverLastName是唯一可以在行与行之间更改的列。 The return type of vehiclesGrp is IQueryable VehicleGrp的返回类型是IQueryable

 var vehiclesGrp = from vehicle in inVehicles group vehicle by new { vehicle.CCName, vehicle.DADDivision, vehicle.DADArea, vehicle.DADDistrict, vehicle.DADCity, vehicle.Vin, vehicle.CostCenter, vehicle.GLDivision, vehicle.VehicleMake, vehicle.FuelTankCapacity, vehicle.FuelType, vehicle.EplanNumber, vehicle.LicensePlate, vehicle.DateInService, vehicle.EstimatedMpg, vehicle.VehicleStatus, vehicle.DriverLastName, } into grp select new VehicleDetail { Account = "", ActualMpg = 0, NoteID = 0, NoteCount = 0, CardNum = "", CCName = grp.Key.CCName, CostCenter = grp.Key.CostCenter, DADArea = grp.Key.DADArea, DADCity = grp.Key.DADCity, DADDistrict = grp.Key.DADDistrict, DADDivision = grp.Key.DADDivision, DateInService = grp.Key.DateInService, DriverLastName = grp.Key.DriverLastName, EplanNumber = grp.Key.EplanNumber, EstimatedMpg = grp.Key.EstimatedMpg, Exception = "", Exceptions = grp.Sum(vehicle => vehicle.Exception == null ? 0 : 1), ExpenseType = "", FuelGals = grp.Sum(vehicle => vehicle.FuelGals), FuelPin = "", FuelTankCapacity = grp.Key.FuelTankCapacity, FuelType = grp.Key.FuelType, FuelUnitCost = 0, GLDivision = grp.Key.GLDivision, IntAcctPd = 0, InvoiceLineItem = "", LicensePlate = grp.Key.LicensePlate, MaintenanceKey = "", Odometer = grp.Max(vehicle => vehicle.Odometer), OutOfServiceDate = null, SwdsStoreCc = "", TotalAllCost = grp.Sum(vehicle => vehicle.TotalAmount), TotalAmount = 0, TotalFuelCost = grp.Sum(vehicle => vehicle.ExpenseType == "FUEL" ? vehicle.TotalAmount : 0), TotalMaintCost = grp.Sum(vehicle => vehicle.ExpenseType == "MAINT" ? vehicle.TotalAmount : 0), TotalMiscCost = grp.Sum(vehicle => vehicle.ExpenseType == "MISC" ? vehicle.TotalAmount : 0), TransactionDate = grp.Max(vehicle => vehicle.TransactionDate), TranTime = "", TranDay = "", UnitCost = 0, VehicleMake = grp.Key.VehicleMake, VehicleStatus = grp.Key.VehicleStatus, VendorAddress = "", VendorCity = "", VendorName = "", VendorState = "", VendorZip = "", Vin = grp.Key.Vin }; return vehiclesGrp; 

If driver "Mike" has transactions on the 31st of the month, and driver "John" does not, then Mike should show up as the driver. 如果驱动程序“ Mike”在当月31日进行交易,而驱动程序“ John”没有,那么Mike应该显示为驱动程序。 If they both show up on the 31st, I don't care which driver, as long as the VIN is not duplicated. 如果它们都出现在31号,只要不复制VIN,我不在乎哪个驱动程序。

Any help on this will be greatly appreciated. 任何帮助,将不胜感激。

Also, if there is any info I left out, let me know, I'll provide what I can. 另外,如果有任何我遗漏的信息,请告诉我,我会尽力提供。

I ended up changing the grouping from what I originally posted to 我最终将分组从最初发布的分组更改为

var vehiclesGrp = (from v in inVehicles.AsParallel().AsQueryable()
                  orderby v.TransactionDate descending 
                     group v by new
                                {
                                 v.CCName,
                                 v.SwdsStoreCc,
                                 v.DADDivision,
                                 v.DADArea,
                                 v.DADDistrict,
                                 v.DADCity,
                                 v.Vin,
                                 v.CostCenter,                                     
                                 v.GLDivision,
                                 v.VehicleMake,
                                 v.FuelTankCapacity,
                                 v.FuelType,
                                 v.EplanNumber,
                                 v.LicensePlate,
                                 v.DateInService,
                                 v.EstimatedMpg,                                    
                                 v.VehicleStatus,                                      
                                }
                      into gp
                      let uniqueVinAnyDriver = gp.Max(v => v.DriverLastName)
                      select new VehicleDetail

which provided exactly what I needed in terms of returning only the "latest" driver of the vehicle. 就只返回“最新”车辆驾驶员而言,这正是我所需要的。

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

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