NHibernate, join between tables and return a specific type

246 views
Skip to first unread message

Christian I.

unread,
Nov 8, 2012, 6:31:40 AM11/8/12
to nhu...@googlegroups.com

I have 2 tables, I'd like place some fields in a result class, but I'm stuck. The code I want look like this with 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();

In Nibernate, at this time, I have this, but I'm stuck ...

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
}


I thirs this too :

I tried the code but I get this error : InvalidOperarionException, variable 'x' of type 'EstimationItem' referenced from scope '', but it is not defined

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


And thid

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

Darren Kopp

unread,
Nov 8, 2012, 10:53:18 PM11/8/12
to nhu...@googlegroups.com
Just use the linq provider and it's the exact same. 

add using NHibernate.Linq and change context.EstimationItem to session.EstimationItem

Darren Kopp

unread,
Nov 8, 2012, 10:53:58 PM11/8/12
to nhu...@googlegroups.com
oops, session.Query<EstimationItem>()
Reply all
Reply to author
Forward
0 new messages