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. This method is called AFTER I use LINQ to get all transactions for all vehicles at a certain location. In the
group vehicle by newsection, DriverLastName is the only column that can change from row to row. The return type of vehiclesGrp is 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. If they both show up on the 31st, I don't care which driver, as long as the VIN is not duplicated.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.