First, you can select decimal values ββfrom objects that satisfy the condition. And then use the .DefaultIfEmpty() method before the .Sum() method:
runTime = db.Records .Where(c => c.MachineDesc.Contains(strMachine) && c.ProductionDate == dt && c.Shift == x) .Select(c => c.RunMinutes) .DefaultIfEmpty() .Sum();
DefaultIfEmpty() function inserts a single element with a default value if the sequence is empty. And, as you know, the value of defualt for type decimal 0.0M . (Table of default values)
Additionally
You did not tell us Linq to What? . But if you use LinqToEntity, then you should change your code, since ( DefaultIfEmpty not supported by EF):
runTime = db.Records .Where(c => c.MachineDesc.Contains(strMachine) && c.ProductionDate == dt && c.Shift == x) .Sum(c => (decimal?)c.RunMinutes) ?? 0;
source share