【发布时间】:2015-01-16 10:43:27
【问题描述】:
我已经阅读了 here 和 here,但我仍然无法弄清楚为什么会出现此错误。 c.NvrId 和 n.Id 的类型都是int
var cameras = from c in context.CameraEntities
join n in context.NetworkVideoRecorderEntities
on c.NvrId equals n.Id
into cn
from x in cn.DefaultIfEmpty()
where listofIds.Contains(c.Id)
select new Camera
{
Id = c.Id,
Name = c.Name,
NetworkVideoRecorder = cn == null ? null : new NetworkVideoRecorder
{
Id = x.Id,
Description = x.Description,
IP = x.IP,
}
};
return cameras.ToList();
当我执行 cameras.ToList(); 时抛出错误
这是完整的错误信息:
Cannot compare elements of type 'System.Collections.Generic.IEnumerable`1[[Models.NetworkVideoRecorderEntity, Asis.Ibss.Net.DataObjects, Version=2014.1.5494.33354, Culture=neutral, PublicKeyToken=null]]'. Only primitive types, enumeration types and entity types are supported.
【问题讨论】:
-
去掉
cn == null ? null :还能用吗? -
附带说明,我认为您应该检查
x是否为空,而不是cn。 -
@RahulSingh ...好地方。
标签: c# .net linq entity-framework