How to do Linq aggregates when there might be an empty set?

前端 未结 4 1768
醉酒成梦
醉酒成梦 2020-12-06 01:13

I have a Linq collection of Things, where Thing has an Amount (decimal) property.

I\'m trying to do an aggregate on this for a

相关标签:
4条回答
  • 2020-12-06 01:39

    If t has a property like a 'HasValue', then I would change the expression to:

    var total = 
         myThings.Where(t => (t.HasValue) && (t.OtherProperty == 123)).Sum(t => t.Amount); 
    
    0 讨论(0)
  • 2020-12-06 01:44

    it throws an exception because the result of the combined sql query is null and this cant be assigned to the decimal var. If you did the following then your variable would be null (I assume ClaimedAmount is decimal):

    var claims = Claim.Where(cl => cl.ID < 0);
    var count = claims.Count(); // count=0
    var sum = claims.Sum(cl => cl.ClaimedAmount as decimal?);
    

    then you should get the functionality you desire.

    You could also do ToList() at the point of the where statement and then the sum would return 0 but that would fall foul of what has been said elsewhere about LINQ aggregates.

    0 讨论(0)
  • 2020-12-06 01:49

    To get a non-nullable result, you need to cast the amount to a nullable type, and then handle the case of Sum returning null.

    decimal total = myThings.Sum(t => (decimal?)t.Amount) ?? 0;
    

    There's another question devoted to the (ir)rationale.

    0 讨论(0)
  • 2020-12-06 01:51

    I can reproduce your problem with the following LINQPad query against Northwind:

    Employees.Where(e => e.EmployeeID == -999).Sum(e => e.EmployeeID)
    

    There are two issues here:

    1. Sum() is overloaded
    2. LINQ to SQL follows SQL semantics, not C# semantics.

    In SQL, SUM(no rows) returns null, not zero. However, the type inference for your query gives you decimal as the type parameter, instead of decimal?. The fix is to help type inference select the correct type, i.e.:

    Employees.Where(e => e.EmployeeID == -999).Sum(e => (int?)e.EmployeeID)
    

    Now the correct Sum() overload will be used.

    0 讨论(0)
提交回复
热议问题