Getting odd/even part of a sequence with LINQ

前端 未结 7 1312
再見小時候
再見小時候 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:11

    If you're using LINQ to SQL or LINQ to Entities you should first fully materialize the results into memory:

    var oddCategories  = projectsByCat.ToList().Where((c,i) => i % 2 != 0);
    var evenCategories = projectsByCat.ToList().Where((c,i) => i % 2 == 0);
    

    It isn't possible to iterate through results on the database with an indexer without the use of a cursor, which either ORM framework does not do.

提交回复
热议问题