什么是nvelocity / C#相当于“如果x在阵”?
题
黑客在Nvelocity C#/。NET视图模板(cs文件),我真的失踪“在” Python的关键字(如“列表中富”)。什么是内置检查列表/数组成员?
这是我的Python的大脑想要做的:
#set ( $ignore = ['a','b','c'] )
<ul>
#foreach ( $f in $blah )
#if ( $f not in $ignore )
<li> $f </li>
#end
#end
</ul>
但我不知道正确的语法是什么,如果有任何确实。我有一个快速浏览一下 Velocity模板指南但没有发现任何有用的东西。
解决方案 2
“包含”确实是我所期待的。
...在NVelocity模板发言:
#set( $ignorefeatures = ["a", "b"] )
#foreach( $f in $blah )
#if ( !$ignorefeatures.Contains($f.Key) )
<tr><td> $f.Key </td><td> $f.Value </td></tr>
#end
#end
其他提示
您可以使用包含在列表功能,所以它应该是
List<int> list = new List<int>{1, 2, 3, 4, 5, 6, 7};
foreach(var f in blah)
if(list.Contains(f))
string[] ignore = {"a", "b", "c" };
foreach( var item in blah ){
if( !ignore.Contains(item) )
{
// do stuff
}
}
如果“忽略”是列表中,有包含()方法就可以了。 这将使你的代码是这样的:
var ignore = new List<string>();
ignore.AddRange( new String[] { "a", "b", "c" } );
foreach (var f in blah) {
if (!ignore.conains(f)) {
//
}
}
可以利用List.Contains
请注意,如果有一个数组可以在阵列投射到IList的,或者创建一个新的列表传递数组。
不知道你会使用什么版本的C#,但如果你有LINQ的,那么你可以在一个阵列上使用Contains
。
不隶属于 StackOverflow