2014-12-02 53 views
0

我在尋求幫助,瞭解MVC 5中的以下內容。我已經閱讀了以前的一些帖子,但是我沒有得到我需要的答案。調用返回Int32內部的方法

我有商店,其中有很多項目,但項目可以是不同的類型。我想在Store/Index視圖中顯示每個商店的所有商店的表格以及不同商品類型的計數。

例如(佈局只是爲了簡單和清晰) 存儲-----#A -----#B -----#C Alder ----- 10 ----- 22 ---- -43 Birmh ----- 07 ----- 11 ----- 89 等

除了Store和Item模型,我還有一個StoreDetails ViewModel,它包含Storename和計數。

在存儲/索引控制器中,我構建了要傳遞到存儲/索引視圖的StoreDetails的列表。

要計算我做在商店/指數控制器以下計數:

public ActionResult Index() 
    { 
     var model = db.Stores 
         .OrderBy(s => s.StoreName) 
         .Select(s => new StoreDetails 
            { 
             ID = s.ID, 
             Name = s.StoreName, 
             CountOfItems = s.Items.Count(), 
             CountOfBigType = BigCount(s.ID) 
             //other counts if i can get this one working 
            }); 
     return View(model); 
    } 

我對BigCount方法是:

public int BigCount(int storeid) 
    { 
     var big = from t in db.Items 
        where t.StoreID == storeID && t.Size > 100 
        select t; 

     return big.Count(); 
    } 

CountOfItems顯示正確的視圖時CountOfBigType()不顯示,但一旦添加我得到Linq實體錯誤。

有什麼建議嗎?

親切的問候 克雷格

回答

3

你不能簡單地修改你的初始LINQ查詢如下,並得到相同的結果:

public ActionResult Index() 
{ 
     var model = db.Stores.OrderBy(s => s.StoreName) 
        .Select (s => new StoreDetails { 
         ID = s.ID, 
         Name = s.StoreName, 
         CountOfItems = s.Items.Count(), 
         CountOfBigType = s.Items.Count(x=>x.Size > 100) <-- 
         //other counts if i can get this one working 
         }); 
     return View(model); 
} 
+0

完美。我不能投票,因爲我是一個低級的「1」!如果我可以的話我會。謝謝。 – 2014-12-02 16:45:32

+0

沒問題,很高興我可以幫忙。 – mreyeros 2014-12-02 18:07:41

+0

@CraigRoberts然而,你可以讓這個接受的答案,你應該這樣做。 – 2014-12-02 18:32:54