2017-08-15 68 views
0
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; } 
} 

細節表的所有列從上面的代碼,我想只選擇ProductIdProductPrimaryPrices的,CodeNameProduct實體Description和所有相關的屬性和ProductSecondaryPrice,因爲它需要很長時間才能從數據庫加載圖像byte[]數組。我只需要Product實體中的選定列和ProductPrimaryPriceProductSecondaryPrices中的所有列。如何選擇唯一入選的主表的列和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(); 

任何人都可以幫我嗎?

+1

您可以選擇一個新的'產品{...}'並設置其屬性那裏。儘管老實說,我認爲一個更清潔的長期解決方案是將字節數組作爲Product的子對象移動到另一個表,並使該屬性延遲加載。 – David

+0

由於您沒有「產品」實體的完整數據,因此我沒有看到您將匿名類型轉換爲「產品」的方式。你是否打算使用這些數據(匿名類型)來最終更新'Product'實體?你究竟想達到什麼目的? – STLDeveloper

+0

感謝@David的評論。我已經嘗試選擇'new Product {...}',但是它給了我一個例外'實體或複雜類型'Product'不能在LINQ to Entities查詢中構造。並且將字節數組移動到另一個表真的是個好主意 –

回答

0

最後,我得到了一些頭腦風暴

現在後一個答案,我能夠通過使用反射匿名類型轉換爲Product類型。我已經實現了一種方法,將匿名類型轉換爲「Product」類型,然後逐個循環匿名類型,並通過調用新實現的方法將其轉換爲Product類型。

下面是一個實現

public List<Product> GetProduct() 
{ 
    List<Product> products = new List<Product>(); 
    using (var ctx = new PosEntities()) 
    { 
     var tempProducts = 
      (from p in ctx.Products.Include("ProductPrimaryPrices").Include("ProductSecondaryPrices").Include("ProductsModifiers") 
      where (p.MenuGroupId == menuGroupId && p.Active) && (p.ProductPrimaryPrices.Any(x => x.OutletId == outletId && x.Active)) 
      select new 
      { 
       ProductId = p.ProductId, 
       Code = p.Code, 
       Name = p.Name, 
       Description = p.Description,     
       ProductPrimaryPrices = p.ProductPrimaryPrices, 
       ProductSecondaryPrices = p.ProductSecondaryPrices, 
       ProductsModifiers = p.ProductsModifiers 
      }).ToList();      

     foreach (object anonymous in tempProducts) 
     { 
      Product product = (Product)Utility.ToType<Product>(anonymous, new Product()); 
      products.Add(product); 
     } 
     return products; 
    } 
} 

以下是方法,匿名類型轉換爲Product

public static class Utility 
    { 
     public static object ToType<T>(this object obj, T type) 
     { 
      //create instance of T type object:    
      object tmp = Activator.CreateInstance(Type.GetType(type.ToString())); 

      //loop through the properties of the object you want to covert:   
      foreach (PropertyInfo pi in obj.GetType().GetProperties()) 
      { 
       try 
       { 
        //get the value of property and try to assign it to the property of T type object: 
        tmp.GetType().GetProperty(pi.Name).SetValue(tmp, pi.GetValue(obj, null), null); 
       } 
       catch (Exception ex) 
       { 
        // Logging.Log.Error(ex); 
       } 
      } 
      //return the T type object:   
      return tmp; 
     }  
    } 
+0

您是否考慮使用David的建議,將您的'new {...}'選擇器更改爲'new Product {...}'?看起來比編寫自己的AutoMapper版本更簡單。因爲'Product'上的屬性重命名不會破壞映射,所以也不太容易出錯。 – StriplingWarrior

+1

@StriplingWarrior我已經嘗試過,但它會拋出一個錯誤,「實體或複雜類型'Product'不能在LINQ to Entities查詢中構造」 –