繁体   English   中英

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

[英]Cannot compare elements of type. Only primitive types, enumeration types and entity types are supported

我已经在这里这里阅读 ,但是我仍然无法弄清楚为什么会出现此错误。 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();时会引发错误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.

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM