我有一个包含四个字段的数据集(DriverNo,Airport,SumAmount和count) 机场可以很多,sumAmount是机场旅行产生的司机数量 我想找出每个司机去机场的行程和他赚了多少。
所以我检索的数据应该很有趣
我的linq就像是跟随我,但我怀疑结果
var list = db.Booking1
.Where(w => w.Gen_Location.LocationTypeId ==
Enums.LOCATION_TYPES.AIRPORT)
.Select(w => new
{
DriverNo = w.Fleet_Driver.DriverNo,
Airport = w.Gen_Location.LocationName,
Amount = w.CustomerPrice,
driverId = w.DriverId
})
.GroupBy(w => w.Airport)
.Select(w => new
{
DriverNo = w.First().DriverNo,
Airport = w.First().Airport,
SumAmount = w.Sum(a => a.Amount),
driverId = w.First().driverId,
count = w.Count()
})
.ToList();
任何帮助将不胜感激
答案 0 :(得分:0)
我想找出每个司机去机场的行程和他赚了多少。
如果是这种情况,那么您还需要按驾驶员分组,现在是您在机场的唯一分组。然后,您可以使用密钥获取所需的数据
var list = db.Booking1
.Where(w => w.Gen_Location.LocationTypeId ==
Enums.LOCATION_TYPES.AIRPORT)
.Select(w => new
{
DriverNo = w.Fleet_Driver.DriverNo,
Airport = w.Gen_Location.LocationName,
Amount = w.CustomerPrice,
driverId = w.DriverId
})
.GroupBy(w => new { w.Airport, w.DriverId})
.Select(w => new
{
// You can put this in the key too but in this case
// I'll grab it from the first element
DriverNo = w.First().DriverNo,
//It's more clear to get this from the key
Airport = w.Key.Airport,
SumAmount = w.Sum(a => a.Amount),
//It's more clear to get this from the key
driverId = w.Key.driverId,
count = w.Count()
})
.ToList();
说实话,所有这些都应该放在自己的类中。如果您需要过滤特定驱动程序的数据,例如4。
list.Where(x => x.driverId == 4);