点击这里给我发消息
点击这里给我发消息
¥1891.00元
智超淘宝店
C#DataTable 使用GroupBy方法的lamda 表达式和Linq语句写法
转载
文章标签 C# WinForm

DataTable ds = new DataTable();

//1、lamda 表达式写法(推荐)
var result = ds.AsEnumerable().GroupBy(s => new{Year = s.Field("Year"), Month = s.Field("Month"), Day = s.Field("Day")});

//2、Linq写法 最终编译器会把它转化为lamda表达式
//var result = from s in ds.Tables[0].AsEnumerable()
             group s by new { Year = s.Field("Year"), Month = s.Field("Month"), Day = s.Field("Day") } into temp

            select temp;

 

//DataTable 使用GroupBy方法需要注意result为IGrouping类型
foreach (var thisGroup in result)
        {
            foreach (var row in thisGroup)
            {
                //遍历当前这组的所有row
            }
        }


转载自:https://www.cnblogs.com/johnblogs/p/6006867.html

突然发现博客园是能复制的,赞!!!