The query below should return records that either have a matching Id supplied in ownerGroupIds or that match ownerUserId. However is ownerUserId is null, I want this part of the query to be ignored.
public static int NumberUnderReview(int? ownerUserId, List<int> ownerGroupIds)
{
return ( from c in db.Contacts
where
c.Active == true
&&
c.LastReviewedOn <= DateTime.Now.AddDays(-365)
&&
( // Owned by user
!ownerUserId.HasValue ||
c.OwnerUserId.Value == ownerUserId.Value
)
&&
( // Owned by group
ownerGroupIds.Count == 0 ||
ownerGroupIds.Contains( c.OwnerGroupId.Value )
)
select c ).Count();
}
However when a null is passed in for ownerUserId then I get the following error: Nullable object must have a value.
I get a tingling I may have to use a lambda expression in this instance?