C# - Lambda 与嵌套循环
是否可以将以下转换为更简单、更易读的 linq 或 lambda 表达式?
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);
}
}
}
}
本质上 doc.TierID 是客户端当前所在的 TierID 的数组 (int[])。此外,doc 对象还包含一个 CommitmentProgram 对象,其中包含 PriceProgram 列表。因此,我要做的就是获取每个 TierID 的 PriceProgram.ProgramID。
PriceProgram和TierID之间的关系是,每个PriceProgram都有一个层列表(ProgramTier对象),并且ProgramTier对象包含我们已经拥有的相应TierID。
如果我的解释没有意义,请告诉我,我会尽力详细说明。
编辑
乔恩, 当我尝试编译您建议的内容时,出现名称“priceProgram”在当前上下文中不存在错误:
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);
Is is possible to convert following into a simpler more readable linq or lambda expression?
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);
}
}
}
}
Essentially doc.TierID is a an array (int[]) of TierIDs that client is currently on. Also the doc object contains a CommitmentProgram object which contains a list of PriceProgram. So, All I am trying to do is get the PriceProgram.ProgramID for each TierID.
The relationship between PriceProgram and TierID is that each PriceProgram has a list of tiers (ProgramTier object) and ProgramTier oject contains the corresponding TierID that we already have.
Let me know if my explaination doesn't make sense and I'll try to elaborate.
Edit
Jon,
I am getting The name 'priceProgram' does not exist in the current context error when I try to compile what you have suggested:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当然,这非常简单 - 但我必须更改您的
selectedProgramTierCombo
变量的类型,否则它将无法编译:编辑:哎呀,鉴于层取决于 PriceProgram,您需要另一个嵌套
from
子句,我认为:至少,这就是我认为你想要的。如果您可以澄清您真正想要的内容,而不是
List
(无效),我们可以提供进一步帮助。老实说,我根本不清楚你为什么要使用
progTier
- 你知道progTier.TierID
与mainTierID
相同,除此之外你没有使用它......Absolutely, it's very easy - but I'll have to change the type of your
selectedProgramTierCombo
variable, as otherwise it won't compile:EDIT: Oops, given that the tiers depends on priceProgram, you need another nested
from
clause, I think:At least, that's what I think you want. If you could clarify what you really want instead of
List<int, int>
(which isn't valid) we can help further.To be honest, it's not clear to me why you're using
progTier
at all - you know thatprogTier.TierID
is the same asmainTierID
, and you're not using it apart from that...乔恩的答案是正确的想法,只是需要重新排列才能编译。这里有两个选择。
Jon's answer is the right idea, just needs to be rearranged in order to compile. Here are two options.
单身有点让我烦恼,但我必须按照要求去做。
这是我会更舒服的:
The Single kind of bugs me, but I have to go with what's being asked for.
This is what I'd be more comfortable with: