【发布时间】:2021-02-11 16:51:10
【问题描述】:
在我的项目中,我有两个类
public class Node
{
public int IdNode { get; set; }
public string Description { get; set; }
public int IdLocation { get; set; }
public int IdNearStation { get; set; }
public bool IsEnable { get; set; }
public bool IsRealSensor { get; set; }
public bool IsSprinklerON { get; set; }
public bool IsLightOn { get; set; }
还有这个
public class DashboardNodeData
{
public Node Node { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public string County { get; set; }
public string DistrictName { get; set; }
}
我想执行这个查询
SELECT N.IdNode
, N.Description
, N.IdLocation
, N.IdNearStation
, N.IsEnable
, N.IsRealSensor
, N.IsSprinklerON
, N.IsLightOn
, N.IsSecurityCameraOn
, L.Latitude
, L.Longitude
, C.Name as County
, D.DistrictName
FROM [dbo].[Node] N
inner join [dbo].[Location] L on N.IdLocation = L.Id_Location
inner join District D on D.Id_District=L.Id_District
inner join Counties C on L.Id_Countie =C.CountyId
where N.IdNode=1
并将此查询的结果映射到 DashboardNodeData 对象
使用 dapper 我这样做
using (IDbConnection db = new SqlConnection(_connectionString))
{
string command = $@"
SELECT N.IdNode
, N.Description
, N.IdLocation
, N.IdNearStation
, N.IsEnable
, N.IsRealSensor
, N.IsSprinklerON
, N.IsLightOn
, N.IsSecurityCameraOn
, L.Latitude
, L.Longitude
, C.Name as County
, D.DistrictName
FROM [dbo].[Node] N
inner join [dbo].[Location] L on N.IdLocation = L.Id_Location
inner join District D on D.Id_District=L.Id_District
inner join Counties C on L.Id_Countie =C.CountyId
where N.IdNode=@idNode".Replace("@idNode",idNode.ToString());
var dashboard = db.Query<DashboardNodeData, Node, DashboardNodeData>(command, (dash, node) =>
{
dash.Node = node;
return dash;
}, splitOn:"Latitude,County").AsList();
return dashboard;
}
问题是在de Query函数里面,我的节点对象总是空的:
但如果我将完全相同的查询复制并粘贴到数据库中,则会返回输出:
我做错了什么?
【问题讨论】: