繁体   English   中英

select语句中的Linq错误

[英]Linq error in the select statement

我有一个返回LINQ语句结果的函数。 请检查引发错误的代码段。

            var result = devices
                .Select(d =>
                        new
                        {
                            deviceName = d.SystemDeviceName,
                            deviceType = d.SystemDeviceTypeName,
                            dvrVersion = d.DVRVersion,
                            numCameras = d.NumCameras,
                            lastPing = d.LastPingDate!=null? d.LastPingDate:null,
                            audioType = d.AudioTypeName ?? "(None)",
                            videoProvider = d.Provider,
                            ipAddress = d.IPAddress,
                            vpnIpAddress = d.VPNIPAddress,
                            internalUseIpAddress = d.InternalUseIPAddress,
                            viewLiveLink = "<a onclick='openViewer(" + d.SystemDeviceID + ", true)'>"
                                    + (ShowIcon ? "<img src='images/video-live.png'>" : String.Empty)
                                    + (ShowIcon && ShowText ? "&nbsp;" : String.Empty)
                                    + (ShowText ? "View Live" : String.Empty)
                                    + "</a>",
                            viewSearchLink = "<a onclick='openViewer(" + d.SystemDeviceID + ", false)'>"
                                    + (ShowIcon ? "<img src='images/video-recorded.png'>" : String.Empty)
                                    + (ShowIcon && ShowText ? "&nbsp;" : String.Empty)
                                    + (ShowText ? "View Recorded" : String.Empty)
                                    + "</a>",
                        })
                .ToArray();

            response.Object = result;
            return response;

我从var result = devices.select语句中得到错误。

错误是:

无法将类型“ System.Int32”强制转换为类型“ System.Object”。 LINQ to Entities仅支持强制转换EDM基本类型或枚举类型。

我试图确定哪个作业引发了错误。 以下几行引起了错误,

viewLiveLink = "<a onclick='openViewer(" + d.SystemDeviceID + ", true)'>"
                                + (ShowIcon ? "<img src='images/video-live.png'>" : String.Empty)
                                + (ShowIcon && ShowText ? "&nbsp;" : String.Empty)
                                + (ShowText ? "View Live" : String.Empty)
                                + "</a>",
                        viewSearchLink = "<a onclick='openViewer(" + d.SystemDeviceID + ", false)'>"
                                + (ShowIcon ? "<img src='images/video-recorded.png'>" : String.Empty)
                                + (ShowIcon && ShowText ? "&nbsp;" : String.Empty)
                                + (ShowText ? "View Recorded" : String.Empty)
                                + "</a>",

设备的等级定义为

public partial class VideoDevice
{
    public VideoDevice()
    {
        this.ImageApeSubscriptions = new HashSet<ImageApeSubscription>();
    }

    public int VideoDeviceID { get; set; }
    public Nullable<int> SiteId { get; set; }
    public string CompanyNumber { get; set; }
    public int SystemDeviceID { get; set; }
    public int CompanySystemID { get; set; }
    public string SystemDeviceName { get; set; }
    public string SystemDeviceTypeName { get; set; }
    public int NumCameras { get; set; }
    public string Provider { get; set; }
    public string DVRVersion { get; set; }
    public string IPAddress { get; set; }
    public string VPNIPAddress { get; set; }
    public string InternalUseIPAddress { get; set; }
    public Nullable<int> PrimaryCameraID { get; set; }
    public Nullable<System.DateTime> LastPingDate { get; set; }
    public string AudioTypeName { get; set; }
    public string ViewerClassName { get; set; }
    public string ViewerName { get; set; }
    public string OverrideIP { get; set; }

    public virtual ICollection<ImageApeSubscription> ImageApeSubscriptions { get; set; }
}

请帮助我清除错误。 提前致谢。

实体框架无法将所有linq查询都转换为sql,但是您可以为此进行预查询。

var result = devices
.ToArray()//<< solution
.Select(d => new{
  ..
})
.ToArray();

希望对您有所帮助。

在将此值作为字符串的一部分放置之前,请尝试将SystemDeviceID转换为字符串

 viewLiveLink = "<a onclick='openViewer(" + d.SystemDeviceID.ToString() + ", true)'>"
                                    + (ShowIcon ? "<img src='images/video-live.png'>" : String.Empty)
                                    + (ShowIcon && ShowText ? "&nbsp;" : String.Empty)
                                    + (ShowText ? "View Live" : String.Empty)
                                    + "</a>",
                            viewSearchLink = "<a onclick='openViewer(" + d.SystemDeviceID.ToString() + ", false)'>"
                                    + (ShowIcon ? "<img src='images/video-recorded.png'>" : String.Empty)
                                    + (ShowIcon && ShowText ? "&nbsp;" : String.Empty)
                                    + (ShowText ? "View Recorded" : String.Empty)
                                    + "</a>",

首先,您使用的是EF6还是EF Core?

其次,关于什么是或可能是什么devices信息很少,如果您能够提供更多的信息,那将是很好的。

第三,尝试隔离错误的指令,以免不必要的/不相关的代码段使它泛滥成灾,您的帖子可能会被重构为包含与您的问题相关的核心信息,您也可以解决它的“橡皮鸭式”风格这样做。

暂无
暂无

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

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