Reputation: 3995
i have a LINQ Query which is written in VB.NET:
foo = (From p In lOrder
Group By p.ItemCode Into Group = Sum(p.Quantity)
).ToDictionary(Function(x) x.ItemCode, Function(x) x.Group)
My problem is to translate the Sum. C# seems to not like it very well.
Can anybody help me translating this query?
thank you
Upvotes: 0
Views: 108
Reputation: 8206
If I understand correctly, you want to get a mapping between the item code to the cumulative quantity of the items under this code.
Given an IEnumerable named orders you can do the following:
var foo = from p in orders
group p by p.ItemCode into OrderGroupedByCode
select new { Code = OrderGroupedByCode.Key, Sum = OrderGroupedByCode.Sum(x => x.Quentity) };
This gives you an IEnumerable of anonymous objects the you can easily turn into a dictionaryif you want:
foo.ToDictionary(x => x.Code, x => x.Sum)
Upvotes: 0
Reputation: 16878
Here's an exact translation of your LINQ query in C#:
var foo = lOrder.GroupBy(p => p.ItemCode,
(key, values) => new
{
ItemCode = key,
Group = values.Sum(v => v.Quantity)
})
.ToDictionary(x => x.ItemCode, x => x.Group);
Upvotes: 2