2017-10-05 67 views
0

我想要獲取數據表中列的不同數量。如何從LINQ查詢中輸出簡單計數(i​​nt)?

我到目前爲止

public int DocumentsProcessed() 
    { 

     var query = from data in this._data 
        let docID = data.Field<string>("Document ID") 
        select new 
        { 
         docID 
        }; 

     var query2 = from d in query 
        select d.docID.Distinct().Count(); 

     var result = query2; 

     return result; 
    } 

以下其中this._data是

private IEnumerable<DataRow> _data; 

但結果是一個IEnumerable。我正在尋找一個單一的整數作爲答案。

編輯:我試圖通過文檔ID列第一組數據,然後計數的羣體,但它給了我打錯電話了 - 比如我有16行,所有的文檔ID都是一樣的,所以含混計數應該是但我得到。

回答

0

所以在你的例子中每一行你正在做一個選擇不同。我相信你想要的是:

var query = from data in this._data 
      let docID = data.Field<string>("Document ID") 
      select new 
      { 
       docID 
      }; 

var distinctCount = query.Distinct().Count(); 
0

我假設數據DataTable對象,從而嘗試以這種方式

int count = _data. 
.AsEnumerable() 
.Select(r => r.Field<string>("Document ID")) 
.Distinct() 
.Count();