I have a query that will give the results in the dictionary <Guid, List<Energy>>
, and then I filter the dictionary results by using the other dictionary key, which is working fine.
Now I would like to apply this filtering right after dictionary results in a single statement, and the result would be tuples.
Below is the query for where I am getting results in the form of a dictionary
var energiesByBenchmarkingEnergyModelTypeId = BenchmarkingModeledEnergyItems
?.GroupBy(o => o.BenchmarkingEnergyModelTypeId)
.ToDictionary(o => o.Key,
o => o.Select(p => new Energy
{
Unit = energyUnitById[p.Energy.UnitId],
Scalar = p.Energy.Scalar
}).ToList());
and below is where I am filtering the dictionary results with other dictionaries and getting the results
List<Energy> proposedEnergies = null;
List<Energy> baselineEnergies = null;
energiesByBenchmarkingEnergyModelTypeId?.TryGetValue(benchmarkingEnergyModelTypeIdByName["Proposed"], out proposedEnergies);
energiesByBenchmarkingEnergyModelTypeId?.TryGetValue(benchmarkingEnergyModelTypeIdByName["Baseline"], out baselineEnergies);
Now would like to get these from the above query, and the sample output looks like the below.
var (proposedEnergies, baselineEnergies) = BenchmarkingModeledEnergyItems
?.GroupBy(o => o.BenchmarkingEnergyModelTypeId)
......
I am trying to figure out how to convert dictionary results to tuples, but I couldn't figure out how to filter the results with the key.
var (proposedEnergies, baselineEnergies) = BenchmarkingModeledEnergyItems
?.GroupBy(o => o.BenchmarkingEnergyModelTypeId)
.ToDictionary(o => o.Key,
o => o.Select(p => new Energy
{
Unit = energyUnitById[p.Energy.UnitId],
Scalar = p.Energy.Scalar
}).ToList())
.Select(a => new Tuple<List<Energy>, List<Energy>>()); // here need to filter out the results and add to the tuples
Could anyone suggest any ideas on this?
Thanks in advance!!!
Well, the following might meet your needs. See what you think.
var proposedModelType = benchmarkingEnergyModelTypeIdByName["Proposed"];
var baselineModelType = benchmarkingEnergyModelTypeIdByName["Baseline"];
var (proposedEnergies, baselineEnergies) = new Tuple<List<Energy>, List<Energy>>(
// Query BMEI, filtering by "Proposed" ModelTypeId
BenchmarkingModeledEnergyItems
.Where(o => o.BenchmarkingEnergyModelTypeId == proposedModelType)
// Make the new "Energy" items.
.Select(p => new Energy
{
Unit = energyUnitById[p.Energy.UnitId],
Scalar = p.Energy.Scalar
})
.ToList(),
// Query BMEI, filtering by "Baseline" Model Type ID
BenchmarkingModeledEnergyItems
.Where(e => e.BenchmarkingEnergyModelTypeId == baselineModelType)
// Make the new "Energy" items.
.Select(p => new Energy
{
Unit = energyUnitById[p.Energy.UnitId],
Scalar = p.Energy.Scalar
})
.ToList()
);
But your original version is probably the simplest solution!