2012-06-02 1 views
0

query.ToList() 내가엔티티 프레임 워크의 쿼리 개체가 개체의 인스턴스로 설정되지

객체 참조가 객체 x.Gallons를 들어

의 인스턴스로 설정되지 얻을 수있는 모든 명령을 호출 할 때 이 값을 설정하면 null이 아닙니다. 또한 적절한 ID로 데이터베이스 테이블에 2 개의 DProduct가 있습니다. 무엇이 잘못 될 수 있습니까?

ProductSummaryCollection.Clear(); 

var query = from p in Repository.repository.ctx.DProduct 
      join fo in Repository.repository.ctx.DFuelOrder.Include("DProduct") 
      on p.ID equals fo.DProductID 
      group fo by fo.DProduct into Prod 
      select new DProductSummary 
      { 
       Product = fo.DProduct, 
       TotalGallons = (float)Prod.Sum(x => x.Gallons) 
      }; 
try 
{ 
    IList<DProductSummary> ps = query.ToList(); 

    foreach (DProductSummary dps in ps) 
     ProductSummaryCollection.Add(dps); 
} 
catch (Exception exc) 
{ 
} 

답변

0

그것은 다음과 같은 2 가지 할 수없는 것 같습니다 :

  1. 은 엔티티 객체를 생성을 DPRODUCT는 LINQ 쿼리 안에 내 경우
  2. 당신은에 대한 참조를 액세스 할 수 없습니다 linq 당신이 그것을 포함하더라도 쿼리 그래서 당신은 조인 table.Propery를 사용해야합니다. 실제로 동작하는 쿼리

    var query = from fo in Repository.repository.ctx.DFuelOrder.Include("DProduct") 
           join p in Repository.repository.ctx.DProduct 
           on fo.DProductID equals p.ID 
           group fo by new { fo.DProductID, p.Name } into Prod 
           select new DProductSummary 
            { 
             ProductName = Prod.Key.Name, 
             TotalGallons = (float)Prod.Sum(x => x.Gallons) 
            }; 
    
관련 문제