I’ve written a simple linq query as follows:
var query = from c in context.ViewDeliveryClientActualStatus
join b in context.Booking on c.Booking equals b.Id
join bg in context.BookingGoods on c.Booking equals bg.BookingId
select new { c, b, bg };
I have filtered the previous query with a number of premises and then needed to group by a set of fields and get the sum of some of them, as so:
var rows = from a in query
group a by new {h = a.c.BookingRefex, b = a.c.ClientRefex, c = a.b.PickupCity, d = a.b.PickupPostalCode} into g
select new
{
Booking_refex = g.Key.h,
Client_refex = g.Key.b,
//Local = g.
Sum_Quan = g.Sum(p => p.bg.Quantity),
};
I’d like to get a few values from a which I haven’t included in the group by clause. How can I get those values? They’re not accessible through g.
The
gin your LINQ expression is anIEnumerablecontaininga'swith an extra propertyKey. If you want to access fields ofathat are not part ofKeyyou will have to perform some sort of aggregation or selection. If you know that a particular field is the same for all elements in the group you can pick the value of the field from the first element in the group. In this example I assume thatchas a field namedValue:However, if
c.Valueis the same within a group you might as well include it in the grouping and access it usingg.Key.cValue.