我有 2 个表,我想在结果类中放置一些字段,但我被卡住了。我想要的代码在 Entity Framework 中看起来像这样:
var res =
(from p in context.EstimationItem
join q in ...
select new EstimationWithDetail
{
Estimation = q.Estimation,
Id = q.Id,
FullName = q.Customer.FirstName + q.Customer.LastName
}).ToList();
在Nibernate中,此时,我有这个,但我被卡住了......
var res =
Session.QueryOver<EstimationItem>()
.JoinQueryOver(x => x.Estimation)
.Select(
x => x.Estimation.Reference,
x => x.Estimation.CreationDate,
x => x.Price,
x => x.Estimation.Customer.FirstName,
x => x.Estimation.Customer.LastName
)
.List();
public class Estimation
{
public virtual string Reference { get; set; }
public virtual Customer Customer { get; set; }
public virtual DateTime CreationDate { get; set; }
public virtual decimal Total { get; set; }
public virtual IList<EstimationItem> EstimationItems { get; set; }
}
public class EstimationItem
{
public virtual decimal Price { get; set; }
public virtual int Quantity { get; set; }
public virtual Estimation Estimation { get; set; }
public virtual Product Product { get; set; }
}
public class EstimationWithDetail
{
public string Reference { get; set; } //Estimation.Reference
public string FullName { get; set; } //concat FirstName and LastName from Customer
public decimal Price { get; set; } //Estimation.Total
public int Id { get; set; } //Estimation.Id
public DateTime CreationDate { get; set; } //Estimation.CreationDate
}
更新 1
我尝试了代码,但出现此错误: InvalidOperarionException,从范围“”引用的“EstimationItem”类型的变量“x”,但未定义
var res =
Session.QueryOver<EstimationItem>()
.JoinQueryOver(x => x.Estimation)
.Select(x => new EstimationWithDetail
{
Code = x.Estimation.Reference,
CreationDate = x.Estimation.CreationDate,
Price = x.Estimation.Total,
FullName = x.Estimation.Customer.FirstName + x.Estimation.Customer.LastName
})
.List<EstimationWithDetail>();
更新2: 我也试过了
Estimation a = null;
Customer b = null;
var res = Session.QueryOver<EstimationItem>()
.JoinAlias(c => c.Estimation, () => a)
.JoinAlias(c => c.Estimation.Customer, () => b)
.Select(x => new EstimationWithDetail
{
Code = a.Reference,
Id = a.Id,
FullName = b.LastName
})
.List<EstimationWithDetail>();