I need help with a linq query
I have a table of Products with names, inventory and other fields. There can be "repeated" product names but with "different inventory".
I need 3 things:
- Select all products and group them by name
- In the SELECT, SUM the inventory field of the products that have the same name
- The Select should be retrieved on an "array" because I will send it through JSON
So for example if I have a product pen with inventory 1.
And then another product with same name "pen" but with inventory 3
Then my select have to give me just 1 row with name "pen" and inventory "4"
I used to have a query like this:
var result = (from c in Products
where c.Inventory != 0
select new[] {
Convert.ToString(i++),
c.Name,
c.Family,
c.Inventory});
It works but now I need to add the "group by" product name and also sum the inventory of each product with the same name but I don't know how