C# DataTable 通過Linq分組


datatable我們是經常使用到的,但是需要對數據進行分組,具體代碼如下:

var result = dt.AsEnumerable().GroupBy(f => new
{
    type = f.Field<string>("type"),
    value = f.Field<string>("value"),
    site_id = f.Field<string>("site_id")
}).Select(f => new
{
    type = f.Key.type,
    value = f.Key.value,
    site_id = f.Key.site_id,
    detect_time = f.Min(x => x.Field<long>("detect_time")),
    data_type = f.Min(x => x.Field<string>("data_type"))
}).ToList();

這樣就實現了分組,返回的result相當於一個dynamic對象。很簡單的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM