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