2012-11-07 4 views
0

2 개의 테이블이 있습니다. 결과 클래스에 일부 필드를 넣고 싶습니다.하지만 막혔습니다. 내가 엔티티 프레임 워크와 같이 원하는 코드 :NHibernate, 테이블들 사이에 조인하고 특정 타입을 반환하십시오.

Nibernate에서
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(); 

,이 시간에, 나는 이것을 가지고,하지만 난 붙어있어 ...

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

는 또한 ..이 문제를 가지고 있고, 그 J의 나는 (내가 추측 한) 피하려고 노력하고있다. 어떤 해결책을 찾았습니까? – Markus

답변

0

내가 NHibernate에 대한 Linq를 제공 잘 알고 아니에요 시도하지만 당신은 뭔가를 할 수 없습니다 같은 : 나는`.List <...>()가`작동 이후에`.Select`을 이동하지만, 다음 SQL 모든 열을 선택합니다 선택하면

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

나는 그 결과로 update1을 시도했다. –

관련 문제