How to force LINQ Sum() to return 0 while source collection is empty

前端 未结 6 1908
余生分开走
余生分开走 2020-12-02 06:17

Basically when I do the following query, if no leads were matched the following query throws an exception. In that case I\'d prefer to have the sum equalize 0 rather than an

6条回答
  •  遥遥无期
    2020-12-02 06:46

    Try this instead, it's shorter:

    db.Leads.Where(..).Aggregate(0, (i, lead) => i + lead.Amount);
    

提交回复
热议问题