We use a SQL database in our ASP.Net Core solution that stores things and can also store zero to multiple item types for each thing. We also store a quantity for each item type stored. I need to be able to sum the quantity for an item type and return it to the View.
Here is what we use in the controller:
IQueryable<Item> items = _context.Items
.Include(ep=>ep.ItemType)
.Include(ep=>ep.ItemActionType)
.Include(ep=>ep.Thing)
.AsQueryable();
The query we use in the Controller to get the sum is:
int itemsum = (int)items.Where(ep => ep.ItemTypeId == 2 && ep.ItemActionTypeId == 8).Select(ep => ep.ItemQuantity).Sum();
The database has the following entries that should result in a sum of six being returned:
Id ThingId ItemTypeId ItemActionTypeId ItemQuantity
1 100 2 8 2
2 100 2 8 1
3 103 2 8 1
4 103 2 8 1
5 105 2 8 1
This information returns a value of 3.
What am I doing wrong?
SOLUTION:
I'm not sure why, but adding in a date range from our Search model allowed us to do the sum operation on the IQueryable. At least, that's what it seems to me. Here is the code we added to make it work. We put this in after the IQueryable, but before the Sum operation.
items = items.Where(e => e.Thing.ThingDateTime >= searchThings.FromDateTime
&& e.Thing.ThingDateTime <= searchThings.ToDateTime);
``