我有以下代码:
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);
}
当我用 brandCode 和 programCode 的值调用它时,我会从数据库中取回预期值。当我拨打电话但将 x.ProgramCode 和 x.BrandCode 显式设置为 null 时,我会从数据库中返回预期的默认值:
ContactEventValue value = ent.ContactEventValues.Single(
x => x.ContactEventTypeID == contactEventType.ContactEventTypeID
&& x.ProgramCode == null && x.BrandCode == null);
但是,当我为 programCode 和 brandCode 调用带有 null 的方法时,我从数据库中返回 null!
我尝试根据此问题的答案将 == 更改为 .Equals():Nullable optional parameter
所以 x.BrandCode.Equals(brandCode) 替换了 x.BrandCode == brandCode,x.ProgramCode.Equals(programCode) 替换了 x.ProgramCode == programCode,但这仍然不起作用。
我也尝试使用 ?? 接线员,还是不行。
这个问题说没有找到解决方案,他/她不得不使用存储过程:EF 4 Query - Issue with Multiple Parameters 我真的不想去那里。
有任何想法吗?