2014-02-21 56 views
0

嗨我有diffuculties試圖得到一個計數。目前它顯示所有記錄但我想要一個計數。從linq查詢獲得計數

using (var ctx = DB.Get()) 
    { 
    Items.AddRange( 
        ctx.Interactions.Where(x => x.ActivityDate >= StartDateTo && 
        x.ActivityDate <= EndDateTo && x.Indepth == true).Select(
        x => new InteractionDTO() 
         {        
          Indepth = x.Indepth 
         } 
        ) 
       ); 
    } 
+3

的'Count'方法? http://msdn.microsoft.com/en-us/library/bb535181(v=vs.110).aspx – asawyer

+0

哪個字段需要計數? –

+0

.Count()應該工作。將它應用到項目列表中,你應該很好去 – AAlferez

回答

2

鑑於您當前的代碼,應該通過將「where」替換爲「count」來獲得計數,如下所示。

return ctx.Interactions.Count(x => x.ActivityDate >= StartDateTo && 
        x.ActivityDate <= EndDateTo && x.Indepth == true) 

UPDATE:要保留當時的結合也許你應該得到的計數關閉您要添加到集合:

using (var ctx = DB.Get()) 
    { 
    Items.AddRange( 
        ctx.Interactions.Where(x => x.ActivityDate >= StartDateTo && 
        x.ActivityDate <= EndDateTo && x.Indepth == true).Select(
        x => new InteractionDTO() 
         {        
          Indepth = x.Indepth 
         } 
        ) 
       ); 

int count = Items.Count(); 
    } 
+1

沒有選擇方法後,用計數 – Master

+1

替換的地方我道歉,這是一個錯字。我只是想幫助。 – alan

+0

我絕對喜歡你採取的方法,比我的方法更清潔。但現在我失去了我的約束力,我如何使用這種方法重新獲得它。 – Master