C# - Lambda 与嵌套循环

发布于 2024-10-07 17:52:23 字数 1322 浏览 6 评论 0原文

是否可以将以下转换为更简单、更易读的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你与昨日 2024-10-14 17:52:23

当然,这非常简单 - 但我必须更改您的 selectedProgramTierCombo 变量的类型,否则它将无法编译:

编辑:哎呀,鉴于层取决于 PriceProgram,您需要另一个嵌套 from 子句,我认为:

Dictionary<int, int> selectedProgramTierCombo =
    (from mainTierID in doc.TierID
     from priceProgram in doc.CommitmentProgram.PricingPrograms
     from progTier in priceProgram.Tiers
     where mainTierID == progTier.TierID
     select new { priceProgram.ProgramID, progTier.TierID })
    .ToDictionary(x => x.ProgramID, x => x.TierID);

至少,这就是我认为你想要的。如果您可以澄清您真正想要的内容,而不是 List (无效),我们可以提供进一步帮助。

老实说,我根本不清楚你为什么要使用 progTier - 你知道 progTier.TierIDmainTierID 相同,除此之外你没有使用它......

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:

Dictionary<int, int> selectedProgramTierCombo =
    (from mainTierID in doc.TierID
     from priceProgram in doc.CommitmentProgram.PricingPrograms
     from progTier in priceProgram.Tiers
     where mainTierID == progTier.TierID
     select new { priceProgram.ProgramID, progTier.TierID })
    .ToDictionary(x => x.ProgramID, x => x.TierID);

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 that progTier.TierID is the same as mainTierID, and you're not using it apart from that...

乙白 2024-10-14 17:52:23

乔恩的答案是正确的想法,只是需要重新排列才能编译。这里有两个选择。

var dict = (from mainTierID in doc.TierID
            join f in
                (from priceProgram in doc.CommitmentProgram.PricingPrograms
                    from progTier in priceProgram.Tiers
                    select new { priceProgram.ProgramID, progTier.TierID })
                on mainTierID equals f.TierID
            select f).ToDictionary(f => f.ProgramID, f => f.TierID);


var dict2 = (from priceProgram in doc.CommitmentProgram.PricingPrograms
                from progTier in priceProgram.Tiers
                join mainTierID in doc.TierID on progTier.TierID equals mainTierID
                select new { priceProgram.ProgramID, progTier.TierID })
            .ToDictionary(x => x.ProgramID, x => x.TierID);

Jon's answer is the right idea, just needs to be rearranged in order to compile. Here are two options.

var dict = (from mainTierID in doc.TierID
            join f in
                (from priceProgram in doc.CommitmentProgram.PricingPrograms
                    from progTier in priceProgram.Tiers
                    select new { priceProgram.ProgramID, progTier.TierID })
                on mainTierID equals f.TierID
            select f).ToDictionary(f => f.ProgramID, f => f.TierID);


var dict2 = (from priceProgram in doc.CommitmentProgram.PricingPrograms
                from progTier in priceProgram.Tiers
                join mainTierID in doc.TierID on progTier.TierID equals mainTierID
                select new { priceProgram.ProgramID, progTier.TierID })
            .ToDictionary(x => x.ProgramID, x => x.TierID);
嘿咻 2024-10-14 17:52:23

单身有点让我烦恼,但我必须按照要求去做。

Dictionary<int, int> selectedProgramTierCombo =
(
  from priceProgram in doc.CommitmentProgram.PricingPrograms
  let tierId =
  (
    from progTier in priceProgram.Tiers
    where doc.TierID.Any(mainTierID => mainTierID == progTier.TierID)
    select progTier.TierID
  ).Single()
  select new
  {
    ProgramID = priceProgram.ProgramID,
    TierID = tierID
  }
).ToDictionary(x => x.ProgramID, x => x.TierID);

这是我会更舒服的:

ILookup<int, int> selectedProgramTierCombo =
(
  from priceProgram in doc.CommitmentProgram.PricingPrograms
  from progTier in priceProgram.Tiers
  where doc.TierID.Any(mainTierID => mainTierID == progTier.TierID)
  select new
  {
    ProgramID = priceProgram.ProgramID,
    TierID = progTier.TierID
  }
).ToLookup(x => x.ProgramID, x => x.TierID);

The Single kind of bugs me, but I have to go with what's being asked for.

Dictionary<int, int> selectedProgramTierCombo =
(
  from priceProgram in doc.CommitmentProgram.PricingPrograms
  let tierId =
  (
    from progTier in priceProgram.Tiers
    where doc.TierID.Any(mainTierID => mainTierID == progTier.TierID)
    select progTier.TierID
  ).Single()
  select new
  {
    ProgramID = priceProgram.ProgramID,
    TierID = tierID
  }
).ToDictionary(x => x.ProgramID, x => x.TierID);

This is what I'd be more comfortable with:

ILookup<int, int> selectedProgramTierCombo =
(
  from priceProgram in doc.CommitmentProgram.PricingPrograms
  from progTier in priceProgram.Tiers
  where doc.TierID.Any(mainTierID => mainTierID == progTier.TierID)
  select new
  {
    ProgramID = priceProgram.ProgramID,
    TierID = progTier.TierID
  }
).ToLookup(x => x.ProgramID, x => x.TierID);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文