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

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -