小编jge*_*man的帖子

使用null参数调用失败

我有以下代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

当我用brandCode和programCode的值调用它时,我从数据库中获得了预期的值.当我进行调用但显式设置x.ProgramCode和x.BrandCode为null时,我从数据库中获取预期的默认值:

ContactEventValue value = ent.ContactEventValues.Single(
        x => x.ContactEventTypeID == contactEventType.ContactEventTypeID
        && x.ProgramCode == null && x.BrandCode == null);
Run Code Online (Sandbox Code Playgroud)

但是,当我为programCode和brandCode调用null方法时,我从数据库中返回null!

我尝试根据此问题的答案将==更改为.Equals():Nullable可选参数

所以x.BrandCode.Equals(brandCode)取代x.BrandCode == brandCode和x.ProgramCode.Equals(的程序代码)所取代x.ProgramCode ==的程序代码,但仍然没有工作.

我也试过用?? 操作员,仍然没有工作.

这个问题说没有找到解决方案,并且他/她必须使用存储过程:EF 4查询 - 多参数问题 我真的不想去那里.

有任何想法吗?

c# linq linq-to-entities entity-framework

6
推荐指数
1
解决办法
1545
查看次数

标签 统计

c# ×1

entity-framework ×1

linq ×1

linq-to-entities ×1