Reputation: 4496
I have a query:
var resultb = unitOfWork.deviceInstanceRepository.Get()
.Select(s => new
{
DeviceId = s.DeviceId,
Name = s.Device.Name,
Manufacturer = s.Device.Manufacturer1.Name,
Quantity = s.Quantity
})
.GroupBy(w => new
{
w.DeviceId,
w.Name,
w.Manufacturer,
w.Quantity
}).ToList();
I want to change this line:
Quantity= s.Quantity
into
Quantity=Sum(s=>s.Quantity)
I found this Click but still have some problems with implementing this. There is no Sum method in Intellisense.
Ok when I change places of Select
and GroupBy
:
var resultb = unitOfWork.deviceInstanceRepository.Get()
.GroupBy(w => new
{
w.DeviceId,
w.Device.Name,
w.Device.Manufacturer,
w.Quantity
})
.Select(s => new
{
DeviceId = s.DeviceId,
Name = s.Device.Name,
Manufacturer = s.Device.Manufacturer1.Name,
Quantity = s.Sum(s=>s.Quantity)
})
I can use Sum
but then lines like : s.DeviceId
throws error that Anonymous type does not contain definition of deviceId
.
Same error for s.Device.Name
and s>device.Manufacuter1.Name
Upvotes: 1
Views: 8910
Reputation: 109079
You must do the grouping without the Quantity
property, because that is the property you want to aggregate in the grouping:
unitOfWork.deviceInstanceRepository.Get()
.GroupBy(w => new
{
DeviceId = w.DeviceId,
Device = w.Device.Name,
Manufacturer = w.Device.Manufacturer,
})
.Select(s => new
{
DeviceId = s.Key.DeviceId,
Name = s.Key.Device,
Manufacturer = s.Key.Manufacturer.Name,
Quantity = s.Sum(x => x.Quantity)
})
Upvotes: 5