Dictionary字典使用的是列举的方法。类似于数学的组合的问题。
排列计算公式:
![](https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D257/sign=97158a763df33a879a6d071ff15d1018/2e2eb9389b504fc2974eb943e2dde71190ef6d66.jpg)
![](https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D70/sign=c20e5a5165d0f703e2b297dc09fa9dc2/4ec2d5628535e5ddaf0ca99971c6a7efce1b6257.jpg)
组合计算公式:
;C(n,m)=C(n,n-m)。(n≥m)
![](https://gss3.bdstatic.com/-Po3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D162/sign=f68c65f4b5b7d0a27fc9009bf9ee760d/5d6034a85edf8db190ab75220e23dd54574e74ea.jpg)
现在做的项目中的一个例子记录下来:
在分布视图PartialView:
@{
var dicBsC = new Dictionary<string,string>{
{"1","长租业务"}, {"2","短租业务"}, {"3","班车业务"}, {"4","跨境业务"},
{ "1,2", "长租业务,短租业务" }, { "1,3", "长租业务,班车业务" }, { "1,4", "长租业务,跨境业务" }, { "2,3", "短租业务,班车业务" },{ "2,4", "短租业务,跨境业务" },{ "3,4", "班车业务,跨境业务" },
{ "1,2,3", "长租业务,短租业务,班车业务" }, { "1,2,4", "长租业务,短租业务,跨境业务" },{"1,3,4","长租业务,班车业务,跨境业务"}, { "2,3,4", "短租业务,班车业务,跨境业务" },
{ "1,2,3,4", "长租业务,短租业务,班车业务,跨境业务" }
};
}
这样就像是数学组合问题:C(4,1)+C(4,2)+C(4,3)+C(4,4) = 4+6+4+1=15
调用字典首先要判断值是否为空以及该值是否属于字典中的值:
@if (!string.IsNullOrEmpty(value) && dicBsC.ContainsKey(value))
{
<span>@dicBsC[value]</span>
}
else
{
<span></span>
}