Answered by:
Sum distinct

Question
-
User2054207217 posted
I have a MVC model that I enumerate into a table. Everything works fine. But, I have a month-to-date value that I need to add, but only add the distinct columns in the footer.
Manager Employee Date Today's Sale MTD Sale John Steve 4/1/2020 12 25 John Steve 4/14/2020 13 25 Will Joe 4/12/2020 4 10 Will Joe 4/23/2020 6 10 Total 35 35 (25+10) I am able to get the Total for Today's Sale by using Model.Sum(i => i.TodaysSale) and it works fine.
How do I accomplish the Total for MTD sale that gets sum of distinct values (25+10) ?
Thanks.
Friday, August 28, 2020 3:16 AM
Answers
-
User1686398519 posted
Hi progdever,
You can use DistinctBy() to achieve your needs.
- You need to import Microsoft.Ajax.Utilities.
- A useful extension method provided by #.NET is Distinct(), but it does not accept the Distinct() overload of Lambda expressions, so you can choose
DistinctBy() if you want to use Lambda expressions.
-
@Model.DistinctBy(t => t.MTDSale).Sum(t => t.MTDSale)
-
More details, you could refer to below code:
Model
public class Test37 { public string Manager { get; set; } public string Employee { get; set; } public string Date { get; set; } public decimal TodaySale { get; set; } public decimal MTDSale { get; set; } }
Controller
public ActionResult Index() { List<Test37> testlist = new List<Test37>(); testlist.Add(new Test37 {Manager= "John", Employee= " Steve ", Date= "4/1/ 2020", TodaySale =12,MTDSale=25}); testlist.Add(new Test37 { Manager = "John", Employee = "Steve", Date = "4/14/ 2020", TodaySale =13, MTDSale =25 }); testlist.Add(new Test37 { Manager = "Will", Employee = "Joe", Date = "4/12/ 2020", TodaySale = 4, MTDSale =10 }); testlist.Add(new Test37 { Manager = "Will", Employee = "Joe", Date = "4/23/ 2020", TodaySale = 6, MTDSale = 10 }); return View(testlist); }
View
@model IEnumerable<WebApplication26.Models.Test37> @using Microsoft.Ajax.Utilities; <table class="table"> <tr> <td>Manager</td> <td>Employee</td> <td>Date</td> <td>Today's Sale</td> <td>MTD Sale</td> </tr> @foreach (var item in Model) { <tr> <td>@item.Manager</td> <td>@item.Employee</td> <td>@item.Date</td> <td>@item.TodaySale</td> <td>@item.MTDSale</td> </tr> } <tr> <td></td> <td></td> <td></td> <td>@Model.Sum(t=>t.TodaySale) </td> <td>@Model.DistinctBy(t => t.MTDSale).Sum(t => t.MTDSale)</td> </tr> </table>
Here is the result.
Best Regards,
YihuiSun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 28, 2020 6:31 AM
All replies
-
User1686398519 posted
Hi progdever,
You can use DistinctBy() to achieve your needs.
- You need to import Microsoft.Ajax.Utilities.
- A useful extension method provided by #.NET is Distinct(), but it does not accept the Distinct() overload of Lambda expressions, so you can choose
DistinctBy() if you want to use Lambda expressions.
-
@Model.DistinctBy(t => t.MTDSale).Sum(t => t.MTDSale)
-
More details, you could refer to below code:
Model
public class Test37 { public string Manager { get; set; } public string Employee { get; set; } public string Date { get; set; } public decimal TodaySale { get; set; } public decimal MTDSale { get; set; } }
Controller
public ActionResult Index() { List<Test37> testlist = new List<Test37>(); testlist.Add(new Test37 {Manager= "John", Employee= " Steve ", Date= "4/1/ 2020", TodaySale =12,MTDSale=25}); testlist.Add(new Test37 { Manager = "John", Employee = "Steve", Date = "4/14/ 2020", TodaySale =13, MTDSale =25 }); testlist.Add(new Test37 { Manager = "Will", Employee = "Joe", Date = "4/12/ 2020", TodaySale = 4, MTDSale =10 }); testlist.Add(new Test37 { Manager = "Will", Employee = "Joe", Date = "4/23/ 2020", TodaySale = 6, MTDSale = 10 }); return View(testlist); }
View
@model IEnumerable<WebApplication26.Models.Test37> @using Microsoft.Ajax.Utilities; <table class="table"> <tr> <td>Manager</td> <td>Employee</td> <td>Date</td> <td>Today's Sale</td> <td>MTD Sale</td> </tr> @foreach (var item in Model) { <tr> <td>@item.Manager</td> <td>@item.Employee</td> <td>@item.Date</td> <td>@item.TodaySale</td> <td>@item.MTDSale</td> </tr> } <tr> <td></td> <td></td> <td></td> <td>@Model.Sum(t=>t.TodaySale) </td> <td>@Model.DistinctBy(t => t.MTDSale).Sum(t => t.MTDSale)</td> </tr> </table>
Here is the result.
Best Regards,
YihuiSun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 28, 2020 6:31 AM -
User2054207217 posted
Hi YihuiSun,
Thanks very much for this example. There is one change I need, instead of distinct on the MTD Sale, how can I do a distinct on the Employee, and then add the MTDSales sum.
I tried:
@Model.DistinctBy(t => t.Manager + t.Employee).Sum(t => t.MTDSale)
@Model.DistinctBy(t=> new{t.Manager , t.Employee}).Sum(t=>t.MTDSale)Both are giving me 60 as total. Thanks.
Friday, August 28, 2020 6:55 PM -
User2054207217 posted
I was able to fix this. Thanks again.
Friday, August 28, 2020 7:45 PM