2

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

如果我的解释没有意义,请告诉我,我会尝试详细说明。

编辑

乔恩,当我尝试编译您所建议的内容时,出现The name 'priceProgram' does not exist in the current context错误:

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);
4

3 回答 3

4

绝对,这很容易 - 但我必须更改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<int, int>(这是无效的),我们可以提供进一步的帮助。

老实说,我不清楚您为什么要使用它progTier-您知道这progTier.TierID与 相同mainTierID,除此之外您没有使用它...

于 2010-12-14T17:48:12.827 回答
2

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

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);
于 2010-12-14T19:09:41.467 回答
1

Single 让我很烦恼,但我必须接受所要求的。

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);
于 2010-12-14T19:46:56.877 回答