i'm a newbie nhibernate,i try use Select in Nhibernate like Entity. This is query
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>();
Error variable 'p' of type referenced from scope, but it is not defined
i don't understand what wrong with it.please help me!
You cannot project with queryover by using linq-to-nhibernate syntax. queryover is not a Linq provider, it is a distinct API with its own semantics. You have to use QueryOver syntax, as illustrated in many answers on Stack Overflow (here by example).
In short, lambda in queryover are mostly used for identifying only one entity property per lambda. (With the exception of Where
conditions being a bit more versatile, but only for simple expressions.) Lambda are not used and supported for expressing elaborated projection in a single lambda, as with Linq. You must decompose it in as many lambda as properties used. Then transform it with a result transformer into your DTO.
Or instead, use linq-to-nhibernate:
using System.Linq;
using NHibernate.Linq; // This using is mandatory with older NHibernate, not with newer NH
// ...
var modelDetailEngine = session.Query<CarSpecification>()
.Where(cs => ...)
.Select(cs => new ModelDetailEngineDto
{
// ...
})
.SingleOrDefault();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With