我有两个通过Entity Framework 4访问的Sql Server表。它们由一个键连接,在连接的从属端有0到1行。这是我的linq查询。
var typeDtoList = from type in typeList
select (new DxStudioTypeDto(parent,
isChildrenLoadOnDemand,
businessFacade,
server,
database,
type.typeGuid,
type.typeName,
type.writerName,
type.managerName,
type.Reporting_Type.MRef_Status,
type.Reporting_Type.Exists_In_Drop));
显然,如果Reporting_Type返回零行,则linq表达式会对DxStudioTypeDto构造函数的最后两个参数抛出空引用异常:
type.Reporting_Type.MRef_Status,
type.Reporting_Type.Exists_In_Drop));
有没有办法检测是否缺少Reporting_Type行并替换默认字符串? DxStudioTypeDto是一个不可变类型,所以当我实例化一个新对象时,我需要所有可用的参数。针对这种情况的任何其他策略?
答案 0 :(得分:0)
你试过这个吗?
((type.Reporting_Type == null) ? "some default value" : type.Reporting_Type.MRef_Status)
我知道有时EF引擎会返回“提供商不支持此操作”这类事情,但值得一试。