I have an inventory usage page that is populated with a list of items I’ve used over the course of a certain period of time (like a month or so). There’s a good amount of items that are duplicates and I am trying to basically put a button on the view page (an IEnumerable List) that will populate a pdf with a list of items and their totals. What I can’t figure out is how to loop through the list of items and if there is any duplicates, get the "AmtTaken" values and sum them together, then remove the duplicates where it only shows each item once but the "AmtTaken" totals are summed together.
I don’t have much code to go off of right now because I’m stuck and am trying to figure out the best way to go about doing this. It’s from a database table called "InventoryUsage".
Here is my model:
public partial class InventoryUsage
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
[Display(Name = "Item Code")]
public string Item_Code { get; set; }
[Display(Name = "Amt Taken")]
public string Amt_Taken { get; set; }
[Display(Name = "Submitted?")]
public Nullable<bool> Submitted { get; set; }
}
So the duplicates I’m looking for is any with the same Item_Code and then if there is a duplicate, then sum the Amt_Taken values together. I also still want it to list all the items that don’t have duplicates. How I have it set currently is with all the items where "Submitted" == false. So with this model, the ID’s won’t be the same if there is a duplicate so that’s another thing that’s throwing me off.
Here is my view page:
@model IEnumerable<MyApp.Models.InventoryUsage>
<div class="wrapper">
<table class="table">
<tr>
<th>
Item/RM Code
</th>
<th>
Amt Taken
</th>
<th>
Submitted? (Y/N)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Item_RM_Code)
</td>
<td>
@Html.DisplayFor(modelItem => item.Amt_Taken)
</td>
<td>
@if (item.Submitted_ == true)
{
<label class="lbl-yes">Yes</label>
}
else
{
<label class="lbl-no">No</label>
}
</td>
<td>
@Html.ActionLink("Generate List", "GetTotals", new { not sure what to put here yet }) |
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
</div>
And for my controller, is where I don’t have much code yet as I am totally stumped.
public ActionResult Index()
{
var usage = from u in db.InventoryUsage
select u;
return View(usage.Where(u => u.Submitted_ == false).ToList());
}
public ActionResult GetTotals()
{
var list = db.InventoryUsage.Where(x => x.Submitted == false).ToList();
//Stuck on this part//
}
Any advice or direction would be greatly appreciated!
2
Answers
I was finally able to get this to work. @dbc and @Moho, thank you both very much! You pointed me in the right direction and after looking into more stuff about grouping methods, I ended up just making a few minor tweaks to your all's suggestions. For some reason, it wasn't letting me do this with my database entity model since it uses a generic list, so I created another model class called UsageTotalsModel and just added the relevant fields from the InventoryUsage Model. I'm guessing since this isn't a database entity model, it can use a generic list that this function uses. Here is my new model I added:
So in my controller, I just pulled the data from InventoryUsage and added it to the UsageTotalsModel. This fixed the error message I kept getting.
Now the only problem I have is that the items with decimal points, are automatically rounding to the nearest tenth so on some of the amounts where it is 0.02 lbs it is showing up as 0. But that's a different question, if I get stuck on it, I'll post another question for that issue, but I should be able to figure it out. Anyways, thanks for the help on this! I would not have ever thought to do the group by and select method. The stuff I found online seemed confusing and wasn't sure if it applied to me, but thanks to you all, you made it make sense!
Use
GroupBy
and sum the results:You didn’t specify on how to deal with mismatches in
Submitted
values for the sameItem_Code
. You can make that part of theGroupBy
key if you want the amounts summed by item code & submitted/not submitted values:Then project those properties: