2012-02-12 37 views
0

即時工作在本網站上,我將所有可用產品放在一個頁面上。每個產品都有自己的ProductDetail(s),顏色,尺寸,價格等。從實體的其他表中獲取最低價格

我想顯示此產品的最低可用價格。我使用帶有產品列表的ViewModel,此時我不知道如何我可以得到這個具體產品的價格。它的產品細節也是一個列表。所以不知何故,我應該得到其ProductDetails的最低價格。

這是我的視圖模型:

var model = new ViewModelProductOverview() 
     { 
      Products = new ProductRepository().GetProductsAvailable(Id, Index, Size), 
      Category = new CategoryRepository().Get(Id), 
      CurrentPageID = page.HasValue ? page.Value : -1 
     }; 

我的觀點:

@{ 
      var i = 0; 

      foreach (var product in Model.Products) 
      { 
       var position = ""; 
       switch (i) 
       { 
        case 0: position = "first"; 
         break; 
        case 1: position = "second"; 
         break; 
        case 2: position = "third"; 
         break; 
        default: i = 0; position = "first"; 
         break; 
       } 

      <li class="@position"><a href="@Url.Action("Detail", "Product", new { productseourl = product.Name.ToSeoUrl(Sign.Minus), productid = product.ID })" > 
      </a> 
       <table> 
        <tr> 
         <td> 
          @product.Name @product.FabricType.Name 
         </td> 
         <td style="text-align: right;"> 
          //Its lowest price need to come here 
         </td> 
        </tr> 
        <tr> 
         <td> 
          @product.SubCategory.Name 
         </td> 
         <td style="text-align: right;"> 
          //Promotion price 
         </td> 
        </tr> 
       </table> 
      </li> 
       i++; 
      } 
     } 
+0

只是代碼一點點(類的輪廓)將使這一個很容易回答。 – 2012-02-12 13:56:41

+0

我已更新代碼示例。 – Wartodust 2012-02-12 14:09:51

+0

ASPX不會有太大的幫助。 – 2012-02-12 17:38:01

回答

1

基本上,

var minPrice = allProducts.Details.Select(d => d.Price).Min(); 

var cheapestDetail = allProducts.Details.OrderBy(d => d.Price).First(); 
+0

問題是,我需要獲取列表中每個產品的這些信息。 – Wartodust 2012-02-12 15:04:36

+0

問題是我們不知道你的課程。 – 2012-02-12 17:38:36

+0

你想看到什麼代碼?我的模特?我爲每個實體使用repositorys來獲取我的集合。 – Wartodust 2012-02-12 19:13:27