【问题标题】:NHibernate, join between tables and return a specific typeNHibernate,表之间连接并返回特定类型
【发布时间】:2012-11-07 20:05:12
【问题描述】:

我有 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>();

【问题讨论】:

  • 我也有这个问题。如果我将.Select 移到.List&lt;...&gt;() 之后,它可以工作,但是SQL 选择会选择所有列,这就是我(我们我猜) 试图避免。你找到解决办法了吗?

标签: c# entity-framework nhibernate orm


【解决方案1】:

我对 NHibernate 的 Linq 提供程序不是很熟悉,因为我没有使用过它,但你不能这样做:

var res = Session.QueryOver<EstimationItem>()
    .JoinQueryOver(x => x.Estimation)
    .Select(x => new EstimationWithDetail
    {
        Estimation = x.Estimation,
        Id = x.Id,
        FullName = x.Estimation.Customer.FirstName + x.Estimation.Customer.LastName

    })
    .List();

【讨论】:

    猜你喜欢
    • 2020-12-17
    • 2017-04-16
    • 1970-01-01
    • 2021-11-05
    • 2018-02-23
    • 2021-11-10
    • 1970-01-01
    • 2013-01-06
    • 2016-03-03
    相关资源
    最近更新 更多