Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

variable of type referenced from scope, but it is not defined

Tags:

nhibernate

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!

like image 564
Huy Pham Avatar asked Mar 09 '23 14:03

Huy Pham


1 Answers

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();
like image 61
Frédéric Avatar answered Apr 06 '23 17:04

Frédéric