Getting odd/even part of a sequence with LINQ

前端 未结 7 1306
再見小時候
再見小時候 2020-12-01 06:47

Say I have a list of all Projects, and that I group them by Category like this:

var projectsByCat = from p in Projects
                     


        
7条回答
  •  一生所求
    2020-12-01 07:03

    The oddCategories and the evenCategories are backward.

    Indexes start a 0 not 1

    0 % 2 = 0

    0 index is odd.

    var oddCategories  = projectsByCat.Where((cat, index) => index % 2 == 0);
    
    var evenCategories = projectsByCat.Where((cat, index) => index % 2 != 0);
    

提交回复
热议问题