从List <dictionary <string,long =“”>&gt;中查找值

时间:2015-09-16 12:08:48

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

我已经获得了PaymentPlans列表,我从中计算出每月$("#myIdContainingTheIp").html()的价格。

  

string = CampaignCode

     

long =每月价格

我无法理解如何在我看来显示当前PaymentPlan的价格。

List<Dictionary<string, long>>

这是我的思绪完全空白的地方。

@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>
}

2 个答案:

答案 0 :(得分:2)

您可以从密钥访问 for(Day days: Day.values()){ System.out.println(days); // printing days } 并在列表之间循环。样本:

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的第一次出现,但在此结构中,您可以有一些。确保它。

答案 1 :(得分:1)

如果List<Dictionary<string, long>>看起来像这样

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"];