The problem is after running
reportData = dbContext.FinancialsBySupplierAuditPeriodStatusType
.Where(v => v.ReviewPeriodID == reportFilter.ReviewPeriodID && v.StatusCategoryID == reportFilter.StatusCategoryID)
.GroupBy(s => new { s.SupplierID })
.Select(g => new DrilldownReportItem {
SupplierID = g.Key.SupplierID,
SupplierName = g.Max(v => v.SupplierName),
AccountNo = g.Max(v => v.AccountNo),
TempTotals = g.Select(v => new TempTotals { ClaimType = v.TypeDesc ?? "Old Claims", Amount = v.Amount })
}).OrderBy(r => r.SupplierName).ToList();
Temp totals is an IEnumerable that contains a IEnumerable of a simple class of
public class TempTotals {
public string Type { get; set; }
public decimal? Amount { get; set; }
}
The idea is to then take this data and group it into a dictionary so that i can get a total of all of the amounts with the key being the type.
The end result should look like:
Dictionary<string, decimal> test = new Dictionary<string, decimal>() {
{"Claim",2 },
{"Query", 500 },
{"Normal", 700 }
};
I know that I could just foreach it however I am looking for a solution using LINQ.
Try this:
Dictionary<string, decimal?> test =
reportData
.SelectMany(rd => rd.TempTotals)
.GroupBy(tt => tt.ClaimType, tt => tt.Amount)
.ToDictionary(g => g.Key, g => g.Sum());
Since the type of Amount
is decimal?
then the dictionary value is also decimal?
.
Try this:
IEnumerable<IEnumerable<TempTotals>> yourCollection;
var dictionary = yourCollection.SelectMany(s => s).ToDictionary(k => k.Type, v => v.Amount);
Where dictionary
will be Dictionary<string, decimal>
. But you need to be sure that you won't have two TempTotals
with the same Type
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With