我试图从IQueryable
中选择一个新对象列表。当IQueryable
中的特定参数为null
时,它会返回列表但该对象。
var companyName = "Company";
var gridObjects = iQueryableObjectList.Select(obj => new GridObject
{
AddedByEmployeeId = obj.AddedByEmployeeId,
AddedByEmployeeSignature = obj.AddedByEmployee.Signature,
AddedDateTime = obj.Created,
DocumentId = obj.DocumentId,
ResponsibleEmployeeId = obj.ResponsibleEmployeeId,
ResponsibleEmployee = obj.ResponsibleEmployeeId != null ?
obj.ResponsibleEmployee.FirstName + " " + obj.ResponsibleEmployee.LastName :
companyName,
Id = obj.Id,
IsCompanyWide = obj.IsCompanyWide
});
iQueryableObjectList
包含6个ObjectItem
类型的对象。其中一个将ResponsibleEmployeeId
值设置为null
。
在此select语句之后,gridObjects
包含5个元素,全部来自iQueryableObjectList
,但ResponsibleEmployeeId
值设置为null
的元素除外。
我提到ResponsibleEmployeeId
在Guid?
和ObjectItem
这两个类中都是GridObject
。
如果我删除
ResponsibleEmployeeId = obj.ResponsibleEmployeeId,
ResponsibleEmployee = obj.ResponsibleEmployeeId != null ?
obj.ResponsibleEmployee.FirstName + " " + obj.ResponsibleEmployee.LastName :
companyName,
从该select语句中返回6个元素。
有没有人对此有所了解?如果需要,请询问其他信息。
编辑:问题解决了。在某些配置中,需要映射该实体ResponsibleEmployeeId
,因此当EF看到它具有空值时,它只是忽略它。
答案 0 :(得分:0)
尝试按obj.ResponsibleEmployeeId != null
obj.ResponsibleEmployeeId != DBNull.Value
var companyName = "Company";
var gridObjects = iQueryableObjectList.Select(obj => new GridObject
{
AddedByEmployeeId = obj.AddedByEmployeeId,
AddedByEmployeeSignature = obj.AddedByEmployee.Signature,
AddedDateTime = obj.Created,
DocumentId = obj.DocumentId,
ResponsibleEmployeeId = obj.ResponsibleEmployeeId,
ResponsibleEmployee = obj.ResponsibleEmployeeId != DBNull.Value ?
obj.ResponsibleEmployee.FirstName + " " + obj.ResponsibleEmployee.LastName :
companyName,
Id = obj.Id,
IsCompanyWide = obj.IsCompanyWide
});
答案 1 :(得分:0)
好的,我和EF有类似的问题,当涉及到将相关实体解析为POCO时。 (GridObject)在你的情况下,我不认为它会使你的源表中有一个带有ResponsibleEmployeeId的FK列的事实绊倒,你可能会仔细检查这个值是实际上是空的.. 请尝试以下方法:
var gridObjects = iQueryableObjectList.Select(obj => new GridObject {
{
AddedByEmployeeId = obj.AddedByEmployeeId,
AddedByEmployeeSignature = obj.AddedByEmployee.Signature,
AddedDateTime = obj.Created,
DocumentId = obj.DocumentId,
ResponsibleEmployeeId = obj.ResponsibleEmployee.EmployeeId
ResponsibleEmployee = obj.ResponsibleEmployee != null ? obj.ReponsibleEmployee.FirstName + " " + obj.ResponsibleEmployee.LastName : companyName,
Id = obj.Id,
IsCompanyWide = obj.IsCompanyWide
});
有一些"怪癖"到Linq2Entities,取决于已经从EF中获取iQueryableObjectList的内容,考虑到它可能还没有实现,但你也可能绊倒其中一个。当尝试使用具有潜在空实体的操作的POCO ViewModel饱和时,我有时会得到NULL引用异常和奇怪的行为。通常我的目的是让我的Queryables在尝试使POCO饱和之前使得到的实体或中间匿名类型饱和。
即。
var gridObjects = iQueryableObjectList.Select(obj => new
{
AddedByEmployeeId = obj.AddedByEmployeeId,
AddedByEmployeeSignature = obj.AddedByEmployee.Signature,
AddedDateTime = obj.Created,
DocumentId = obj.DocumentId,
ResponsibleEmployeeId = obj.ResponsibleEmployeeId,
ResponsibleEmployee = obj.ResponsibleEmployee,
Id = obj.Id,
IsCompanyWide = obj.IsCompanyWide
}).ToList() // Saturates the entity data incl. Responsible Employee...
.Select( obj => new GridObject
{
AddedByEmployeeId = obj.AddedByEmployeeId,
AddedByEmployeeSignature = obj.AddedByEmployee.Signature,
AddedDateTime = obj.Created,
DocumentId = obj.DocumentId,
ResponsibleEmployeeId = obj.ResponsibleEmployeeId,
ResponsibleEmployee = obj.ResponsibleEmployee != null
? obj.ResponsibleEmployee.FirstName + " " + obj.ResponsibleEmployee.LastName
: companyName,
Id = obj.Id,
IsCompanyWide = obj.IsCompanyWide
});
对于视图模型,我们通常会公平地保持它们#34;平坦"相对于回来的数据。例如,在上面的示例中,我考虑使用FirstName& GridObject中的LastName列以及CompanyName具有名为DisplayName的计算属性。从Linq2EF中填充POCO的相关位将变为:(选择新的GridObject { ...
FirstName = obj.ResponsibleEmployee.FirstName, // EF automatically handles cases where ResponsibleEmployee is Null
LastName = obj.ResponsibleEmployee.LastName,
CompanyName = companyName,
... 然后在GridObject中有一个DisplayName,它是根据EmployeeId和/或Name数据的可用性计算的。