linq - C# get every n-th element with WHERE, strange result -


using system; using system.collections.generic; using system.linq;  public class test {     public static void main()     {         list<int> list = new list<int>();         for(int = 0; < 16; ++i) list.add(i);         console.writeline(string.join(" ", list.where((o, i) => % 4 == 0).select((o, i) => i).toarray()));     } } 

can explain, why code above returns 0 1 2 3 instead of 0 4 8 12?

because selecting index instead value. try this:

console.writeline(string.join(" ", list.where(o =>o % 4 == 0).select((o, i) => o).toarray())); 

if not going nothing index this:

console.writeline(string.join(" ", list.where(o => o % 4 == 0).toarray())); 

Comments

Popular posts from this blog

wireshark - USB mapping with python -

c++ - nodejs socket.io closes connection before upgrading to websocket -

Deploying Qt Application on Android is really slow? -