【问题标题】:Find value from List<Dictionary<string, long>>从 List<Dictionary<string, long>> 中查找值
【发布时间】:2015-12-13 00:23:17
【问题描述】:

我有一个 PaymentPlans 列表,并从中计算出每月的价格 List&lt;Dictionary&lt;string, long&gt;&gt;

字符串 = 活动代码

long = 每月价格

我不知道如何在我的视图中显示当前 PaymentPlan 的价格。

@Model.PaymentPlans.PricePerMonth.FirstOrDefault(x => x.)

这是我脑子里一片空白的地方。

public class PaymentPlansIncPPM
{
    public List<PaymentPlan> PaymentPlans { get; set; }
    public List<Dictionary<string, long>> PricePerMonth { get; set; }
}


@foreach (var plan in Model.PaymentPlans)
{
    <div>
        @Html.RadioButton("CampaignId", plan.CampaignCode, new { @id = plan.CampaignCode })
        <label for="@plan.CampaignCode"></label>
        <span>
            <b>@plan.ContractLengthInMonths months</b><br />
            
<-- Here is where I want to show the price per month -->

             $/month
        </span>
    </div>
}

编辑: 字典包含一个或多个,总是匹配计划的数量。 (如果有帮助的话)

Key "campaignCode"  string
Value   100000  long

Key "pricePerMonth" string
Value   42  long

【问题讨论】:

  • @NedStoyanov 据我了解 - xDictionary,所以它没有 Key 属性
  • 不要将long用于财务/金钱价值,使用decimal
  • 字典列表中有什么?你想要这个吗? var correctDict = Model.PricePerMonth.First(pm => pm.Kesy.Any(k => k == plan.CampaignCode)); var pricePerMonth = correctDict[plan.CampaignCode];
  • @ArghyaC 更新了字典信息
  • 这行得通吗? var correctDict = Model.PricePerMonth.First(dict => dict["campaignCode"] == plan.CampaignCode); var pricePerMonth = correctDict["pricePerMonth"];

标签: c# asp.net-mvc list dictionary


【解决方案1】:

您可以通过键访问Dictionary,并在列表之间循环。示例:

@foreach (var plan in Model.PaymentPlans)
{
    <div>
        @Html.RadioButton("CampaignId", plan.CampaignCode, new { @id = plan.CampaignCode })
        <label for="@plan.CampaignCode"></label>
        <span>
            <strong>@plan.ContractLengthInMonths months</strong><br />
            @{
                var prices = plan.PricePerMonth.FirstOrDefault(d => d.ContainsKey(plan.CampaignCode));

               if (prices != null)
               {
                   foreach(long price in prices[plan.CampaignCode])
                   {
                       <text>
                       <li>@price</li>
                       </text>
                   }    
               }

            }           
             $/month
        </span>
    </div>
}

假设你的模型上有一个字典列表,你可以找到一个字典,其中有一个 Key 等于你的 CampaignCode 并显示这个 Key 的值。我更改了代码以显示CampaignCode 的第一次出现,但在这个结构中,你可以有一些。确定一下。

【讨论】:

  • 我不确定 OP 是否需要字典中的 all 值,而不仅仅是选定的 CampaignCode
  • 是的,就像@Grundy 说的那样。我只需要当前计划的价格。
  • @Rob,所以在列表中你只有 一个 字典包含CampaignCode 或者它可以是几个?
  • 感谢 cmets 伙计们。我已经编辑了我的答案。我做了一个类似上一个的代码,因为在像List&lt;Dictionary&lt;&gt;&gt; 这样的结构中,你可以有几个值。我知道你可以准备你的代码来避免它,但结构允许它:)
  • @Grundy 一个或多个,总是匹配计划的数量。
【解决方案2】:

如果List&lt;Dictionary&lt;string, long&gt;&gt; 看起来像这样

PricePerMonth (list) :

dict1: [
        {Key: "campaignCode", Value:  100000},
        {Key: "pricePerMonth", Value: 42}
    ]
dict2: [
        {Key: "campaignCode", Value:  100001},
        {Key: "pricePerMonth", Value: 29}
    ]
dict3: [
        {Key: "campaignCode", Value:  100002},
        {Key: "pricePerMonth", Value: 51}
    ]

您可以获得活动的“每月价格”(plan.CampaignCode)为

var correctDict = Model.PriceMonth.First(dict => dict["campaignCode"] == plan.CampaignCode);
var pricePerMonth = correctDict["pricePerMonth"];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多