NHibernate查询的问题

时间:2010-02-24 09:17:52

标签: c# asp.net-mvc nhibernate

我目前正参与零件目录项目。

为了给你一些背景信息,我有3个nHib实体部分,应用程序和车辆。

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

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

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; }

}

可以看出,零件可以有很多应用程序,应用程序可以有很多车辆。

我试图使用Make,Model,Type和Engine拉回所有车辆的列表,但也要突出显示是否有任何车辆链接到给定的应用程序。我打算使用具有make,model,type,engine和islinked(bool)属性的DTO。

我可以将过滤后的车辆拉回来,但是我遇到了识别车辆是否与应用程序相关联的问题。如果我可以做以下

那就太好了
IsLinked = ((Vehicle.Applications.Count(x => x.Name == _name) > 0)

但它没有编译。任何想法??

此致

3 个答案:

答案 0 :(得分:1)

最初我使用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();
}

但是在看完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;
    }

哪位有效,谢谢你的帮助。

答案 1 :(得分:0)

我不明白你的RTO代码是什么。但也许这有助于弄明白。

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();
}

答案 2 :(得分:0)

使用标准查询。

如果您想找到具有特定应用的车辆:

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

或者,不是在applicationname上进行过滤,而是需要基于它的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();