I am trying to get the below linq query to return -1 if there isn’t any current value. I was looking at this article on MSDN, here, and it seemed that DefaultIfEmpty() was what I wanted.
Unfortunately, I am getting a The type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly. error.
I guess I am not sure what that means or what it is telling me to do. Can someone explain, please.
public static int CheckForDRIID(int personID)
{
using (var context = ConnectDataContext.Create())
{
var masterIndex =
(from applicationAssociation in context.tblApplicationAssociations
where applicationAssociation.ApplicationID == 1 && applicationAssociation.PersonID == personID
select applicationAssociation.PersonApplicationID).DefaultIfEmpty(-1).Single();
return Convert.ToInt32(masterIndex);
}
}
-1 is an int and
applicationAssociation.PersionApplicationIDisn’t, so it doesn’t know what to return. You could replace -1 with the same type asapplicationAssociation.PersionApplicationID, “-1”. Or specify the type likeDefaultIfEmpty<string>. And a third options is to do theConvert.ToInt32in the select.