Est-il possible de convertir à la suite en une expression linq ou lambda plus lisible?C# - Lambda vs boucles imbriquées
Dictionary<int, int> selectedProgramTierCombo = new Dictionary<int,int>();
foreach (int mainTierID in doc.TierID)
{
foreach (PriceProgram priceProgram in doc.CommitmentProgram.PricingPrograms)
{
foreach (ProgramTier progTier in priceProgram.Tiers)
{
if (progTier.TierID == mainTierID)
{
selectedProgramTierCombo.Add(priceProgram.ProgramID, progTier.TierID);
}
}
}
}
Essentiellement doc.TierID est un un réseau (int []) de ce client TierIDs est actuellement. L'objet doc contient également un objet CommitmentProgram qui contient une liste de PriceProgram. Donc, tout ce que j'essaie de faire est d'obtenir le PriceProgram.ProgramID pour chaque TierID.
La relation entre PriceProgram et TierID est que chaque PriceProgram a une liste de niveaux (Objet ProgramTier) et ProgramTier oject contient le TierID correspondant que nous avons déjà. Faites-moi savoir si mon explication n'a pas de sens et je vais essayer d'élaborer.
Modifier
Jon, Je reçois Le nom « priceProgram » n'existe pas dans le contexte actuel erreur lorsque je tente de compiler ce que vous avez suggéré:
Dictionary<int, int> selectedProgramTierCombo =
(from mainTierID in doc.TierID
from priceProgram in doc.CommitmentProgram.PricingPrograms
**join progTier in priceProgram.Tiers on mainTierID equals progTier.TierID**
select new { priceProgram.ProgramID, progTier.TierID })
.ToDictionary(x => x.ProgramID, x => x.TierID);
Utilisation du type générique 'System.Collections.Generic.List' exige '1' arguments de type. –
ja72
J'ai fourni le code mis à jour basé sur la réponse initiale de Jon. –
J'ai édité ma réponse pour utiliser une autre clause 'from' au lieu de' join' ... bien qu'il semble que vous n'ayez pas du tout besoin de ce bit, vraiment. –