2010-02-24 85 views
1

我目前涉及零件目錄項目。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和islinked(bool)屬性的DTO。

我可以把過濾的車輛拉回來,但我有問題確定車輛是否與應用程序鏈接。這將是很好,如果我可以做以下

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

但它不編譯。有任何想法嗎??

問候

豐富

+0

你不能做Vehicle.Applications.Contains(vehicle)嗎? :) – Skurmedel

回答

0

我不明白什麼是您的DTO的代碼。但也許這將有助於弄清楚。

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

從技術上講,我的目標不是一個DTO,但它在這裏。 public class ApplicationVehicleSummary { public string Make {get;組; } public string Model {get;組; } public string Type {get;組; } public string Engine {get;組; } public bool IsLinked {get;組; } } –

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

這有用,謝謝你的幫助。