简体   繁体   中英

Find value from List<Dictionary<string, long>>

I've got a list of PaymentPlans and from that I calculated a List<Dictionary<string, long>> of price per month.

string = CampaignCode

long = Price Per Month

I can't get my head around on how to show the price for current PaymentPlan in my view.

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

This is where my mind is completely blank.

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

edit: The Dictionary contains one or more, always matching the number of plans. (if it's any help)

Key "campaignCode"  string
Value   100000  long

Key "pricePerMonth" string
Value   42  long

You could access the Dictionary from the key and loop between the list. For sample:

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

Given you have a list of dictionaries on your Model, you can find the a dictionary where is there a Key is equals your CampaignCode and show the values of this Key . I change the code to show for the first occurrence of the CampaignCode but in this structure, you can have a few. Make sure about it.

If the List<Dictionary<string, long>> looks like this

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

You can get the 'price-per-month' of the campaign (plan.CampaignCode) as

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM