C# select语句中的Linq错误
我有一个函数,它返回LINQ语句的结果。请检查抛出错误的代码段C# select语句中的Linq错误,c#,entity-framework,linq,C#,Entity Framework,Linq,我有一个函数,它返回LINQ语句的结果。请检查抛出错误的代码段 var result = devices .Select(d => new { deviceName = d.SystemDeviceName, deviceType
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 ? " " : 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 ? " " : String.Empty)
+ (ShowText ? "View Recorded" : String.Empty)
+ "</a>",
})
.ToArray();
response.Object = result;
return response;
设备的防护等级为
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; }
}
公共部分类视频设备
{
公共视频设备()
{
this.ImageApeSubscriptions=new HashSet();
}
公共int VideoDeviceID{get;set;}
公共可为空的SiteId{get;set;}
公共字符串CompanyNumber{get;set;}
公共int SystemDeviceID{get;set;}
public int CompanySystemID{get;set;}
公共字符串SystemDeviceName{get;set;}
公共字符串SystemDeviceTypeName{get;set;}
public int NumCameras{get;set;}
公共字符串提供程序{get;set;}
公共字符串DVRVersion{get;set;}
公共字符串IPAddress{get;set;}
公共字符串VPNIPAddress{get;set;}
公共字符串InternalUseIPAddress{get;set;}
公共可为空的PrimaryCameraID{get;set;}
公共可为空的LastPingDate{get;set;}
公共字符串AudioTypeName{get;set;}
公共字符串ViewerClassName{get;set;}
公共字符串ViewerName{get;set;}
公共字符串重写IP{get;set;}
公共虚拟ICollection映像订阅{get;set;}
}
请帮我清除这个错误。提前感谢。在将此值作为字符串的一部分之前,请尝试将
SystemDeviceID
转换为字符串
viewLiveLink = "<a onclick='openViewer(" + d.SystemDeviceID.ToString() + ", true)'>"
+ (ShowIcon ? "<img src='images/video-live.png'>" : String.Empty)
+ (ShowIcon && ShowText ? " " : 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 ? " " : String.Empty)
+ (ShowText ? "View Recorded" : String.Empty)
+ "</a>",
viewLiveLink=“”
+(ShowIcon?“:String.Empty)
+(ShowIcon&&ShowText?“:String.Empty)
+(ShowText?“实时查看”:String.Empty)
+ "",
viewSearchLink=“”
+(ShowIcon?“:String.Empty)
+(ShowIcon&&ShowText?“:String.Empty)
+(ShowText?“记录的视图”:String.Empty)
+ "",
首先,您使用的是EF6还是EF Core
其次,我们几乎没有关于设备是什么或可能是什么的信息,如果您能提供更多信息,那就太好了
第三,试着隔离错误的指令,这样它就不会被不必要的/不相关的代码膨胀,你的帖子可能会被重构,以包含与你的问题相关的核心信息,您也可以在执行此操作时以“橡皮鸭”式解决此问题。实体框架无法将所有linq查询转换为sql,但您可以预先查询以执行此操作
var result = devices
.ToArray()//<< solution
.Select(d => new{
..
})
.ToArray();
var结果=设备
.ToArray()//新建{
..
})
.ToArray();
我希望这能有所帮助。代码中的数据类型对话问题。请检查您的数据类型。您使用哪个实体框架版本?尝试显式地将d.SystemDeviceID
转换为字符串(d.SystemDeviceID.ToString()
)。我从var result=devices.select语句中得到错误。
作为一般LINQ规则,如果得到运行时错误,请逐行从LINQ查询中取出位。当错误停止时,您刚才删除的位是有问题的。请帮自己一个忙,将数据和UI问题分开。让数据库查询生成html是不好的。对于每个微小的UI更改,您必须更改一个查询(测试等)。首先获取数据,然后进行UI操作。这是我在发布此问题之前尝试过的。但这给了我错误。我刚才注意到的一点是,当我在一个select中选择Id,然后在另一个select中框显HTML时,它就工作了。我的EF版本是4.0.0.0
var result = devices
.ToArray()//<< solution
.Select(d => new{
..
})
.ToArray();