简体   繁体   English

NHibernate查询问题

[英]Problems with an NHibernate query

I am currently involved in a parts catalogue project. 我目前正在参与零件目录项目。

To give you some background info, I have 3 nHib Entities Part, Application and Vehicle. 为了给您一些背景信息,我有3个nHib实体部分,应用程序和车辆。

Part.cs Part.cs

public class Part : Entity
{
    public Part()
    {
        Quality = new PartQuality();
        FitmentPosition = new FitmentPosition();
        OEPartNumbers = new List<OEPartNumber>();
        Applications = new List<Application>();
    }

    public virtual string Description { get; set; }

    [NotNull]
    [NotEmpty]
    [Pattern(@"\d{4}[-]\d{3}$", RegexOptions.None, "Must be a valid part number")]
    [DomainSignature]
    public virtual string PartNumber { get; set; }

    public virtual PartQuality Quality { get; set; }

    public virtual FitmentPosition FitmentPosition { get; set; }

    public virtual string Notes { get; set; }

    public virtual string VehicleComments { get; set; }

    public virtual string Image { get; set; }

    public virtual IList<OEPartNumber> OEPartNumbers { get; set; }

    public virtual IList<Application> Applications { get; set; }
}

Application.cs Application.cs

public class Application : Entity
{
    [DomainSignature]
    public virtual string Name { get; set; }

    public virtual DateTime DateFrom { get; set; }
    public virtual DateTime DateTo { get; set; }

    // Fuel
    public virtual bool Diesel { get; set; }
    public virtual bool Petrol { get; set; }

    // Transmission
    public virtual bool Manual { get; set; }
    public virtual bool Automatic { get; set; }
    public virtual bool SemiAutomatic { get; set; }

    // Air Con
    public virtual bool WithAC { get; set; }
    public virtual bool WithOutAC { get; set; }

    // Body
    public virtual bool Hatchback { get; set; }
    public virtual bool Saloon { get; set; }
    public virtual bool Convertable { get; set; }
    public virtual bool Estate { get; set; }
    public virtual bool Coupe { get; set; }
    public virtual bool Van { get; set; }

    // Number of Doors
    public virtual bool Doors2 { get; set; }
    public virtual bool Doors3 { get; set; }
    public virtual bool Doors4 { get; set; }
    public virtual bool Doors5 { get; set; }

    [DomainSignature]
    public virtual Part Part { get; set; }

    public virtual IList<Vehicle> Vehicles { get; set; }
}

Vehicle.cs Vehicle.cs

public class Vehicle : Entity
{
    public virtual string Make { get; set; }

    public virtual string Model { get; set; }

    public virtual string Type { get; set; }

    public virtual string Engine { get; set; }

    public virtual DateTime ProductionStart { get; set; }

    public virtual DateTime ProductionEnd { get; set; }

    public virtual IList<Application> Applications { get; set; }

}

It can be seen that a part can have many applications and an application can have many vehicles. 可以看出,一个零件可以有很多应用程序,而一个应用程序可以有很多车辆。

I am trying to pull back a list of all vehicles, using Make, Model, Type and Engine, but also highlight if any of the vehicles are linked to a given Application. 我正在尝试使用“品牌”,“型号”,“类型”和“引擎”来拉回所有车辆的列表,但还要强调是否有任何车辆链接到给定的“应用程序”。 I was going to use a DTO with properties of make, model, type, engine and islinked(bool). 我将使用具有make,model,type,engine和islinked(bool)属性的DTO。

I can pull back the filtered vehicles fine, but I am having the issue of identifying whether the vehicle is linked to an application or not. 我可以将经过过滤的车辆罚款,但我遇到了确定车辆是否链接到应用程序的问题。 It would be nice if I could do the following 如果可以执行以下操作会很好

IsLinked = ((Vehicle.Applications.Count(x => x.Name == _name) > 0)

But it doesn't compile. 但是它不能编译。 Any Ideas?? 有任何想法吗??

Regards 问候

Rich 丰富

Originally I was writting the query using ICritreia (like Lachlan), 最初,我是使用ICritreia(例如Lachlan)编写查询的,

public override IQueryable<ApplicationVehicleSummary> GetQuery(ISession session)
{
        ICriteria criteria = session.CreateCriteria<Vehicle>();

        // SELECT
        criteria
            .SetProjection(
            Projections.Property("Make"),
            Projections.Property("Model"),
            Projections.Property("Type"),
            Projections.Property("Engine")
            );
        // WHERE
        criteria
            .Add(
            Restrictions.Eq("Make", _criteria.Make) &&
            Restrictions.Eq("Model", _criteria.Model) &&
            Restrictions.Eq("Type", _criteria.Type) &&
            Restrictions.Eq("Engine", _criteria.Engine)
            );

        //criteria.Add(Something("IsLinked",Subqueries.Gt(0,subCriteria)));

        criteria.SetResultTransformer(Transformers.AliasToBean<ApplicationVehicleSummary>());

        return criteria.List<ApplicationVehicleSummary>().AsQueryable();
}

But after reading Cem's post decided to use a Linq query. 但是在阅读Cem的帖子后,决定使用Linq查询。

public override IQueryable<ApplicationVehicleSummary> GetQuery(ISession session)
    {
        var results = session.Linq<Vehicle>()
            .Select(v => new ApplicationVehicleSummary
                             {
                                 Make = v.Make,
                                 Model = v.Model,
                                 Type = v.Type,
                                 Engine = v.Engine,
                                 IsLinked = v.Applications.Any(a => a.Name == _name)
                             })
            .Where(v =>
                   v.Make == _criteria.Make &&
                   v.Model == _criteria.Model &&
                   v.Type == _criteria.Type &&
                   v.Engine == _criteria.Engine
            );
        return results;
    }

Which works, thanks for your help. 哪个可行,谢谢您的帮助。

I did not understood whats your DTO code. 我不了解您的DTO代码是什么。 but maybe it will help to figure out. 但也许会有所帮助。

public class DTO
{
    public bool IsLinked { get; set; }
}

public IList<DTO> Get(string _name)
{
    return Session.Linq<Vehicle>()
            .Select(v => new DTO
                             {
                                 IsLinked = v.Applications.Any(a => a.Name == _name)
                             })
            .ToList();
}

Using criteria queries. 使用条件查询。

If you wanted to find those vehicles with a particular application: 如果要查找具有特定应用程序的那些车辆:

result = session.CreateCriteria<Vehicle>()
    .CreateAlias( "Applications", "a" )
    .Add( Expression.Eq( "Make ", make ) )
    .Add( Expression.Eq( "a.Name", applicationname ) )
    .List<Vehicle>();

Or, instead of filtering on applicationname, you want a DTO flag based on it: 或者,您不想基于应用程序名称进行过滤,而是需要基于它的DTO标志:

vehicles = session.CreateCriteria<Vehicle>()
    .Add( Expression.Eq( "Make ", make ) )
    .List<Vehicle>();

dto = vehicles
        .Select(v => new DTO
        {
             IsLinked = v.Applications.Any(a => a.Name == applicationname )
        })
        .ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM