我是nbieernate的新手,我尝试在像实体这样的Nhibernate中使用Select。 这是查询
var modelDetailEngine = session.QueryOver<CarSpecification>(() => specificationAlias)
.Left.JoinAlias(() => specificationAlias.Car, () => carAlias)
.Left.JoinAlias(() => carAlias.Year, () => yearAlias)
.Left.JoinAlias(() => yearAlias.Model, () => modelAlias)
.Left.JoinAlias(() => modelAlias.Style, () => styleAlias)
.Left.JoinAlias(() => styleAlias.Manufacturer, () => manufactureAlias)
.Where(() => manufactureAlias.Id == manufactureId && modelAlias.Id == modelId && yearAlias.Id == yearId)
.Select(p => new ModelDetailEngineDto()
{
EngineName = p.Engine,
EngineType = p.Type_Engine,
CompressionRatio = p.Compresstion,
DrivingType = p.Driving_Type,
TranmissionType = p.Transmission_Type,
FuelType = p.Fuel_Type,
FuelEconomyCity = p.Fuel_Economy_City,
FuelEconomyHighway = p.Fuel_Economy_Highway,
Locking = p.Locking,
EngineValvetrain = p.Engine_Valvetrain,
ESS = p.ESS,
EBD = p.EBD,
RemoteVehicle = p.Remote_Vehice,
Tranmission = p.Transmission,
ExteriorLength = p.Exterior_Length,
ExteriorWidth = p.Exterior_Width,
ExteriorHeight = p.Exterior_Height,
HorsePower = p.Horsepower,
Torque = p.Torque,
DragCoeficient = p.Drag_Coeficient,
TimeSpeed = p.TimeSpeed,
Km = p.Km,
CurbWeight = p.Curb_Weight,
GVWR = p.GVWR
}).SingleOrDefault<ModelDetailEngineDto>();
从作用域引用的错误变量'p',但未定义
我不明白它有什么问题。请帮助我!
答案 0 :(得分:4)
您无法使用queryover语法与linq-to-nhibernate进行投影。 queryover不是Linq提供者,它是一个具有自己语义的独特API。你必须使用QueryOver syntax,如Stack Overflow上的许多答案所示(例如here)。
简而言之,queryover中的lambda主要用于识别每个lambda只有一个实体属性。 (Where
条件除了simple expressions之外更具通用性,但仅限于linq-to-nhibernate。)Lambda不用于支持在单个lambda中表达精细投影,与Linq一样。您必须在与使用的属性一样多的lambda中对其进行分解。然后使用结果转换器将其转换为DTO。
或者使用Here:
using System.Linq;
using NHibernate.Linq;
// ...
var modelDetailEngine = session.Query<CarSpecification>()
.Where(cs => ...)
.Select(cs => new ModelDetailEngineDto
{
// ...
})
.SingleOrDefault();