public partial class Product
{
public Product()
{
this.ProductPrimaryPrices = new HashSet<ProductPrimaryPrice>();
this.ProductSecondaryPrices = new HashSet<ProductSecondaryPrice>();
}
public int ProductId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public byte[] Image { get; set; }
public virtual ICollection<ProductPrimaryPrice> ProductPrimaryPrices { get; set; }
public virtual ICollection<ProductSecondaryPrice> ProductSecondaryPrices { get; set; }
}
細節表的所有列從上面的代碼,我想只選擇ProductId
ProductPrimaryPrices
的,Code
,Name
和Product
實體Description
和所有相關的屬性和ProductSecondaryPrice
,因爲它需要很長時間才能從數據庫加載圖像byte[]
數組。我只需要Product
實體中的選定列和ProductPrimaryPrice
和ProductSecondaryPrices
中的所有列。如何選擇唯一入選的主表的列和Linq中
我已經嘗試過使用匿名類型,如下所示,但如何再次將匿名類型轉換回Product
。
var tempProduct = (from p in ctx.Products.Include("ProductPrimaryPrices").Include("ProductSecondaryPrices")
select new
{
ProductId = p.ProductId,
Code = p.Code,
Name = p.Name,
Description = p.Description,
ProductPrimaryPrices = p.ProductPrimaryPrices,
ProductSecondaryPrices = p.ProductSecondaryPrices
}
).ToList();
任何人都可以幫我嗎?
您可以選擇一個新的'產品{...}'並設置其屬性那裏。儘管老實說,我認爲一個更清潔的長期解決方案是將字節數組作爲Product的子對象移動到另一個表,並使該屬性延遲加載。 – David
由於您沒有「產品」實體的完整數據,因此我沒有看到您將匿名類型轉換爲「產品」的方式。你是否打算使用這些數據(匿名類型)來最終更新'Product'實體?你究竟想達到什麼目的? – STLDeveloper
感謝@David的評論。我已經嘗試選擇'new Product {...}',但是它給了我一個例外'實體或複雜類型'Product'不能在LINQ to Entities查詢中構造。並且將字節數組移動到另一個表真的是個好主意 –