1

在我的簡單數據庫第一個實體框架場景中,我有客戶將價目表編號分配給他們。另一方面,有一個產品表有7個與價目表相對應的價格欄,例如, PR_1,PR_2,PR_3等動態構建Linq中的選擇字段到實體

我想對幾個屬性進行投影。問題在於其中一個屬性/字段取決於客戶的價目表編號,因此這需要動態構建。因此,如果價目表編號是2,那麼要使用的屬性是PR_2等等。

public IList<Product> GetSalesList(int customerId) 
{ 
    Customer customer = this.customerService.Get(customerId); 

    var products = this.productRepository.GetAll().OrderBy(p => p.ProductCode) 
      .Select(p => new Product 
      { 
       Id = p.Id, 
       ProductCode = p.ProductCode, 
       Description = p.Description, 
       PricingTypeCode = p.PricingTypeCode, 
       Quantity = 0, 
       SalesPrice = <<PR_ + customer.PricelistNumber.ToString()>> 
      }).ToList(); 

    return products; 
} 

我已閱讀關於DLINQ,但我不想使用該原因,我認爲這只是一個例外。

任何人都可以指向正確的方向或爲此做某人的解決方案嗎?

回答

0

我會將所有價格存儲在Product中,然後根據客戶的價目表編號計算銷售價格的方法SalesPrice

喜歡的東西:

public decimal SalesPrice(Customer customer) 
{ 
    return prices[customer.PriceListNumber - 1]; 
} 
+0

謝謝,我覺得這是一個很好的替代,但在我的情況了'Product'是DTO。所以這種方法將用於服務。我仍然更喜歡選擇字段的動態構造。 –