0
我用下面的实体框架代码优先pattern.I'm数据模型的新的LINQ Lambda表达式获取最低价格清单列表使用Lambda表达式
问题:
我想计算单行程已在ItineraryPackagePrice
public class Region
{
public int RegionID { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
public string Name { get; set; }
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
public string Type { get; set; }
public int? ParentID { get; set; }
public virtual Region Parent { get; set; }
public virtual ICollection<Itinerary> Itinerarys { get; set; }
}
public class Itinerary
{
public int ItineraryID { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
public string Name { get; set; }
[StringLength(50, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
public string QuoteReferenceNo { get; set; }
public virtual ICollection<Region> Regions { get; set; }
public virtual ICollection<ItineraryPackagePrice> ItineraryPackagePrices { get; set; }
}
public class ItineraryPackagePrice
{
public int ItineraryPackagePriceID { get; set; }
[DataType(DataType.Currency)]
[Column(TypeName = "money")]
public decimal Price { get; set; }
public int ItineraryID { get; set; }
public virtual Itinerary Itinerary { get; set; }
}
Lambda表达式最低价格值:
var db = new dbContext();
var siblingRegions = db.Regions.Where(s => s.ParentID == Model.Region.RegionID).OrderBy(s => s.Name);
// iterating on each resion
foreach (var subreg in siblingRegions)
{
var singleItinerary = subreg.Itinerarys.
Single(obj =>
new {
Min=obj.ItineraryPackagePrices.
Min(k=>k.Price)});
//how can I write a query here to calculate minimum price
}
我不知道你想要什么:一个查询来获取'singleItinerary'每个'siblingRegions'而不是每个项目一个查询? – ChrFin 2015-01-21 11:27:11
不,我想计算单个区域的单一行程,即在foreach语句中的子区域 – Sam 2015-01-21 11:29:14
那么你有什么错误? – ChrFin 2015-01-21 11:29:58