无法比较类型的元素。仅支持基本类型,枚举类型和实体类型

时间:2015-01-16 10:43:27

标签: c# .net linq entity-framework

我已经阅读了herehere,但我仍然无法理解为什么我会收到此错误。 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.

1 个答案:

答案 0 :(得分:0)

您正在'select'子句中进行空检查。

  

NetworkVideoRecorder = cn == null ? null : new NetworkVideoRecorder { Id = x.Id, Description = x.Description, IP = x.IP, }

正如@RahulSingh在评论中指出的那样,我认为你最好检查x是否为null而不是检查cn是否为空。