2012-03-04 146 views
0

我在LINQ to SQL中有一個工作查詢。我需要將它轉換爲LINQ to Entities。LINQ to Entities中的子查詢

(from pr in Products 
join c in Categories on pr.CategoryID equals c.CategoryID 
join b in Colors on pr.ColorID equals b.ColorID 
select new{ 
ProductID = pr.ProductID, 
Manufacturer = pr.Manufacturer.Name, 
Model = v.Model.ModelName, 
Category = c.Name, 
Photos = from p in Photos where pr.ProductID == p.ProductID select p 
}).FirstOrDefault() 

當我使用它,因爲它是我得到一個錯誤:

Unable to create a constant value of type Only primitive types ('such as Int32, String, and Guid') are supported in this context.

的問題是在這個部分:

Photos = from p in Photos where pr.ProductID == p.ProductID select p 

回答

0

我終於解開了它在很簡單的方法 - 我分在查詢到2種方法:

GetAllProducts() 
GetAllPhotos() 

,比加入通過ProductsDTO具有來自兩個查詢的所有屬性的結果。

1

LINQ到實體自動管理的相關查詢您(假設您的所有密鑰/關係都已正確定義)。

試試這個:

(from pr in Products 
select new{ 
ProductID = pr.ProductID, 
Manufacturer = pr.Manufacturer.Name, 
Model = pr.Color.Model.ModelName, 
Category = pr.Category.Name, 
Photos = pr.Photos 
}).FirstOrDefault() 

也看到這篇文章(http://msdn.microsoft.com/en-us/library/bb896321.aspx)