DataTable Linq Group Count where写法

    DataTable dataTable = new DataTable();
    dataTable.Columns.Add("username", typeof(string));

    dataTable.Rows.Add("zgj");
    dataTable.Rows.Add("zgj");
    dataTable.Rows.Add("zgj1");
    dataTable.Rows.Add("zgj1");
    dataTable.Rows.Add("zgj1");

    var query = from t in dataTable.AsEnumerable()
                group t by new { t1 = t.Field<string>("username") } into m
                select new
                {
                    username = m.Key.t1,
                    usernamecount = m.Count()
                };

    foreach (var q in query)
    {
        Console.WriteLine(q.username + ":" + q.usernamecount);
    }

输出结果

zgj:2
zgj1:3

带where 语句写法

var query = from t in dataTable.AsEnumerable()
            where t["username"].ToString()=="zgj"
            group t by new { t1 = t.Field<string>("username") } into m
            select new
            {
                username = m.Key.t1,
                usernamecount = m.Count()
            };

 

posted @ 2019-08-02 18:04  zhaogaojian  阅读(741)  评论(0编辑  收藏  举报