首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NHibernate,在表之间联接并返回特定类型

NHibernate,在表之间联接并返回特定类型
EN

Stack Overflow用户
提问于 2012-11-07 20:05:12
回答 1查看 398关注 0票数 0

我有两个表,我想把一些字段放在一个结果类中,但是我被卡住了。我想要使用实体框架的代码如下所示:

代码语言:javascript
运行
复制
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,现在,我有这个,但是我被困住了.

代码语言:javascript
运行
复制
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,变量'x‘类型'EstimationItem’从作用域‘引用’,但它没有定义

代码语言:javascript
运行
复制
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 :我也尝试过

代码语言:javascript
运行
复制
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>();
EN

回答 1

Stack Overflow用户

发布于 2012-11-07 20:11:12

我不太熟悉NHibernate的Linq提供程序,因为我还没有使用它,但是您不能这样做:

代码语言:javascript
运行
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13277324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档