I have the following code:
public static ContactEventValue GetContactEventValue(ContactEventType contactEventType, string programCode, string brandCode)
{
AdvocacyEntities ent = AdvocacyEntities.GetReadOnlyInstance();
ContactEventValue value = ent.ContactEventValues.SingleOrDefault(
x => x.ContactEventTypeID == contactEventType.ContactEventTypeID
&& x.ProgramCode == programCode && x.BrandCode == brandCode);
}
When I call it with values for brandCode and programCode, I get the expected value back from the database. When I make the call but explicitly setting x.ProgramCode and x.BrandCode to null I get the expected default value back from the database:
ContactEventValue value = ent.ContactEventValues.Single(
x => x.ContactEventTypeID == contactEventType.ContactEventTypeID
&& x.ProgramCode == null && x.BrandCode == null);
However, when I call the method with null for programCode and brandCode, I get null back from the database!
I tried changing the == to .Equals() per the answer to this issue: Nullable optional parameter
So x.BrandCode.Equals(brandCode) replaced x.BrandCode == brandCode, and x.ProgramCode.Equals(programCode) replaced x.ProgramCode == programCode, but that still didn't work.
I also tried using the ?? operator, still didn't work.
This issue says an solution wasn't found, and s/he had to used a stored procedure: EF 4 Query - Issue with Multiple Parameters I really don't want to have to go there.
Any ideas?