Linq All on empty collection

后端 未结 8 2139
面向向阳花
面向向阳花 2021-01-08 00:24

I need to check if all definitions contains some specific data. It works fine except the case when GroupBy returns empty collection.

var exist = dbContext.De         


        
8条回答
  •  死守一世寂寞
    2021-01-08 01:12

    You may be able to do this using an Aggregate, along the lines of:

    .Aggregate(new {exists = 0, matches = 0}, (a, g) =>
            new {exists = a.exists + 1, matches = a.matches + g > 10 ? 1 : 0})
    

    (Here, g > 10 is my test)

    And then simple logic that exists is greater than zero and that exists and matches have the same value.

    This avoids running the whole query twice.

提交回复
热议问题