2012-06-02 35 views
0

當調用query.ToList()我得到實體框架查詢:對象不設置到對象的實例

對象引用不設置到對象

對於x.Gallons的一個實例,所有的訂單有這個值設置,它不是null。此外,數據庫表中有2個具有正確ID的DProducts。什麼可能是錯的?

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

看來,你不能做以下兩兩件事:

  1. 創建一個實體對象,DPRODUCT在我的情況LINQ查詢裏面
  2. 您不能訪問的一個參考linq查詢,即使你包含它 所以你必須使用join 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) 
            }; 
    
相關問題