Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从列表中查找值<;字典<;长字符串>&燃气轮机;_C#_Asp.net Mvc_List_Dictionary - Fatal编程技术网

C# 从列表中查找值<;字典<;长字符串>&燃气轮机;

C# 从列表中查找值<;字典<;长字符串>&燃气轮机;,c#,asp.net-mvc,list,dictionary,C#,Asp.net Mvc,List,Dictionary,我有一份付款计划清单,从中我计算出每月的价格清单 字符串=活动代码 长期=每月价格 在我看来,我不知道如何显示当前付款计划的价格 @Model.PaymentPlans.PricePerMonth.FirstOrDefault(x => x.) 这就是我头脑完全空白的地方 public class PaymentPlansIncPPM { public List<PaymentPlan> PaymentPlans { get; set; } public Li

我有一份付款计划清单,从中我计算出每月的价格清单

字符串=活动代码

长期=每月价格

在我看来,我不知道如何显示当前付款计划的价格

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

您可以从列表之间的键和循环访问
字典
。对于示例:

@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>
}
@foreach(Model.PaymentPlans中的var计划)
{
@RadioButton(“CampaignId”,plan.CampaignCode,新的{@id=plan.CampaignCode})
@plan.ContractLengthInMonths月
@{ var prices=plan.PricePerMonth.FirstOrDefault(d=>d.ContainsKey(plan.activitycode)); 如果(价格!=null) { foreach(价格中的长期价格[计划.活动代码]) {
  • @价格
  • } } } 美元/月 }
    如果您的模型上有一个字典列表,您可以找到一个字典,其中有一个
    等于您的
    活动代码
    ,并显示该
    键的值。我将代码更改为在第一次出现
    activitycode
    时显示,但在此结构中,您可以使用一些。请确认。

    如果
    列表类似于此

    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)


    @据我所知,
    x
    字典
    ,因此它没有
    属性。对于金融/货币值,不要使用
    ,请使用
    十进制
    。字典列表中有什么?你想要这个吗?var correctDict=Model.PricePerMonth.First(pm=>pm.Kesy.Any(k=>k==plan.activitycode));var pricePerMonth=correctDict[plan.activitycode]@ArghyaC更新了字典信息,这有用吗?var correctDict=Model.PricePerMonth.First(dict=>dict[“活动代码”]==plan.activitycode);var pricePerMonth=正确的dict[“pricePerMonth”];我不确定OP是否需要字典中的所有值,而不仅仅是选中的
    activitycode
    Yea,就像@Grundy所说的那样。我只需要当前计划的价格。@Rob,所以在列表中您只有一本包含
    活动代码的字典,或者可以是一些?谢谢各位的评论。我已经编辑了我的anwser。我编写了与上一个类似的代码,因为在类似
    List
    的结构中,可以有几个值。我知道您可以编写代码来避免它,但结构允许:)@Grundy一个或多个,始终与计划数匹配。
    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}
        ]
    
    var correctDict = Model.PriceMonth.First(dict => dict["campaignCode"] == plan.CampaignCode);
    var pricePerMonth = correctDict["pricePerMonth"];