"对象引用未设置为对象的实例",即使我检查了null

Maj*_*jid 2 .net c# linq null reference

我有RoomBedEF类,每个Room都有一些Beds.when我使用这个LINQ语句:

IEnumerable<Room> room=...
if (room == null || !room.Any())
    return null;
return room.SelectMany(r=>r.Beds); 
Run Code Online (Sandbox Code Playgroud)

给我这个错误:

你调用的对象是空的.

return行.

YK1*_*YK1 15

可枚举中的一个房间是null.做这个:

return room.Where(r => r != null)
           .SelectMany(r => r.Beds);
Run Code Online (Sandbox Code Playgroud)