I found out that my Collection of Room s were not null and also none of Room 's Beds were null . problem was that at least one item of my RoomCollection was null . so according to YK1's answer I should use:
return room.Where(r => r != null).SelectMany(r => r.Beds);
IEnumerable<Room> room=...
if (room == null) // Check for nulls
return null;
else if (room.count() == 0) // Check if empty
return null;
else if(!room.Any(x => x.Beds == null) // Check if there is no null Beds
return room.SelectMany(r=>r.Beds);
5条答案
按热度按时间vmpqdwk31#
可枚举对象中有一个房间为空。请执行以下操作:
mutmk8jj2#
I found out that my
Collection
ofRoom
s were notnull
and also none ofRoom
'sBeds
werenull
. problem was that at least one item of myRoom
Collection
wasnull
. so according to YK1's answer I should use:lp0sw83n3#
仅当Room具有
Beds == null
时,才会发生此错误。你说:“我只有一个房间,里面有两张床”,但问题也提到了EF。
那么问题就出在
...
中的IEnumerable<Room> room=...
。当EF查询使用lazy loading时,即使存在记录,Beds属性也将为空。
要获得完整的解决方案,您必须发布有关EF部件的所有详细信息:编码|首先,查询DB类型、Context类、EF版本等。
在EF的最新版本中,这种问题很少见,我猜您的查询中有一个不应该出现的
ToList()
。5q4ezhmt4#
您也可以尝试使用count,如下所示:
ugmeyewa5#
请试试这个...