What is the nvelocity / C # equivalent of "if x is in an array"?

Hacking on the Nvelocity C # /. NET presentation template (.cs file), I really miss the Python keyword "in" (like in "foo in list"). What is the inline element for checking list / array?

This is what my Python brain wants to do:

#set ( $ignore = ['a','b','c'] )
<ul>
#foreach ( $f in $blah )
  #if ( $f not in $ignore )
    <li> $f </li>
  #end
#end
</ul>

But I'm not sure what the correct syntax is, if it really is. I quickly looked through the Speed ​​Template Guide , but found nothing useful.

+3
source share
6 answers

“Contains” is really what I was looking for.

... And in the NVelocity-talk template:

#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
+3
source

,

List<int> list = new List<int>{1, 2, 3, 4, 5, 6, 7};
foreach(var f in blah)
if(list.Contains(f))
+4
string[] ignore = {"a", "b", "c" };
foreach( var item in blah ){
    if( !ignore.Contains(item) )
    {
        // do stuff
    }
}
+1

"ignore" - , Contains(). :

var ignore = new List<string>();
ignore.AddRange( new String[] { "a", "b", "c" } );
foreach (var f in blah) {
    if (!ignore.conains(f)) {
        //
    }
}
0

You can use List.Contains

Note that if you have an array, you can either pass the array to IList or create a new List that passes the array.

0
source

Not sure which version of C # you are using, but if you have Linq you can use Containsin an array.

0
source

Source: https://habr.com/ru/post/1713618/


All Articles