I have the following code where I need to execute 2 Linq-to-SQL statements. If I do not have the Any()
in the if
statement, then I receive error when the result is no data. However, I am not sure if my approach is efficient enough. Do you have any suggestions?
My goal is to achieve the following with single Linq statement. I need to get the Sum
, and get 0 if there are no results.
if (db.BadgeAssignments.Any(bba => bba.UserIdReceiver == newbadgeassignment.UserIdReceiver && bba.BadgeAssociated.CourseId == courseId))
{
currUserScore = db.BadgeAssignments
.Where(bba => bba.UserIdReceiver == newbadgeassignment.UserIdReceiver && bba.BadgeAssociated.CourseId == courseId)
.Sum(bba => bba.BadgeAssociated.Points);
}