# Calculating the .Sum() in Linq-to-SQL

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))
{
}


You have:

if (db.BadgeAssignments.Any(bba => bba.UserIdReceiver == newbadgeassignment.UserIdReceiver && bba.BadgeAssociated.CourseId == courseId))


And later:

.Where(bba => bba.UserIdReceiver == newbadgeassignment.UserIdReceiver && bba.BadgeAssociated.CourseId == courseId)


The two lambdas:

bba => bba.UserIdReceiver == newbadgeassignment.UserIdReceiver && bba.BadgeAssociated.CourseId == courseId


Identical. .Any is $O(n)$, meaning worst-case, it will have to iterate all elements of the source to find one that matches the predicate; Where is also $O(n)$, meaning it will traverse all elements of the source to find all that match the predicate.

currUserScore = db.BadgeAssignments


This does it once, and will return 0 if no match is made while iterating BadgeAssignments... and that leaves you with a single linq statement =)

The predicate could be simplified further if you encapsulated it into a separate method:

currUserScore = db.BadgeAssignments
.Where(bba => IsMatch(bba, userId, courseId))

The IsMatch function (//todo: find a better name) is the lambda's body:
return bba.UserIdReceiver == userId

• IsMatch won't work. EF will be unable to translate it into SQL and will crash. The OP put two mutually exclusive tags. With EF it will only work if you call AsEnumerable after the collection. – t3chb0t Nov 10 '16 at 18:02