[英]Problems with an NHibernate query
我目前正在參與零件目錄項目。
為了給您一些背景信息,我有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)
但是它不能編譯。 有任何想法嗎??
問候
豐富
最初,我是使用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;
}
哪個可行,謝謝您的幫助。
我不了解您的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();
}
使用條件查詢。
如果要查找具有特定應用程序的那些車輛:
result = session.CreateCriteria<Vehicle>()
.CreateAlias( "Applications", "a" )
.Add( Expression.Eq( "Make ", make ) )
.Add( Expression.Eq( "a.Name", applicationname ) )
.List<Vehicle>();
或者,您不想基於應用程序名稱進行過濾,而是需要基於它的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.