C#DataTable 使用GroupBy方法的lamda 表达式和Linq语句写法

https://www.cnblogs.com/johnblogs/p/6006867.html

DataTable ds = new DataTable();

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

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

select temp;

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

//统计不重复的数量,没测试不知是否能用

dt.DefaultView.ToTable(true, new string[1] { "UserName" }).Rows.Count;

http://www.xuebuyuan.com/1990057.html

using (DataTable dt = ds.Tables[0])
        {
            //三个变量分别记录总记录数、不重复的用户数、所有用户的金额总额
            int rowsCount, distinctUserRowsCount, AllUserMoney;
            rowsCount = dt.Rows.Count;
            distinctUserRowsCount = dt.DefaultView.ToTable(true, new string[1] { "UserName" }).Rows.Count;
            AllUserMoney = Convert.ToDecimal(dt.Compute("sum(UserMoney)", ""));
        }

原文地址:https://www.cnblogs.com/LuoEast/p/8799161.html

时间: 2024-08-05 04:58:59

C#DataTable 使用GroupBy方法的lamda 表达式和Linq语句写法的相关文章

匿名方法与Lamda表达式

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 匿名方法与Lamda表达式 { class Program { static void Main(string[] args) { //任何可以使用委托的地方都可以使用匿名方法,匿名方法就是没有名字的方法. //a指向一个匿名方法 Action a

C#高级知识点概要(3) - 特性、自动属性、对象集合初始化器、扩展方法、Lambda表达式和Linq查询

1.特性(Attributes) 特性(Attributes),MSDN的定义是:公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型.字段.方法和属性等.Attributes和Microsoft .NET Framework文件的元数据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为.例如,在一个方法前标注[Obsolete]特性,则调用该方法时VS则会提示该方法已过期的警告,如下图: 又如,在.Net Re

MVC用户登录方法(lamda表达式)

? ? public bool ValidateUser(account model) { using (assertEntities db = new assertEntities()) { account result = db.account.FirstOrDefault(m => m.name == model.name); if (result != null) { if (result.password == model.password) { return true; } else

EF的连表查询Lambda表达式和linq语句(转)

var lst = from c in db.Blogs join p in db.Posts on c.Id equals p.BlogId where p.Id==1 select c; var lst1 = db.Blogs.Join(db.Posts.Where(p=>p.Id==1), b=> b.Id, p=> p.BlogId, (b, p) => new {b}); public class Blog { public int Id { get; set; } pu

c# Linq及Lamda表达式应用经验之 GroupBy 分组

本文转载自:http://www.cnblogs.com/han1982/p/4138163.html 示例1: GroupBy 分组在List<>泛型中的应用 原表: 按姓名Name 分组后结果: 对DATATABLE 进行LAMDA查询时必须在项目的引用中添加 System.Data.DataSetExtensions 代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } List

(转)c# Linq及Lamda表达式应用经验之 GroupBy 分组

本文转载自:http://www.cnblogs.com/han1982/p/4138163.html 示例1: GroupBy 分组在List<>泛型中的应用 原表: 按姓名Nam 分组后结果: 对DATATABLE 进行LAMDA查询时必须在项目的引用中添加 System.Data.DataSetExtensions 代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } List&

JAVA8新特性——Lamda表达式

JAVA9都要出来了,JAVA8新特性都没搞清楚,是不是有点掉队哦~ Lamda表达式,读作λ表达式,它实质属于函数式编程的概念,要理解函数式编程的产生目的,就要先理解匿名内部类. 先来看看传统的匿名内部类调用方式: interface MyInterface{ void lMethod(); } public class Main { public static void test(MyInterface myInterface){ myInterface.lMethod(); } publi

[.net 面向对象程序设计进阶] (5) Lamda表达式(二) 表达式树快速入门

[.net 面向对象程序设计进阶] (6) Lamda表达式(二) 表达式树快速入门 本节导读: 认识表达式树(Expression Tree),学习使用Lambda创建表达式树,解析表达式树. 学习表达式在程序设计中的优点:比如构造动态查询.动态构造表达式树完成未知对象属性访问,比反射的性能高出很多.我们可以说表达式树才是Lambda的精髓,是我们必须要熟练掌握并灵活运用的. 1.关于表达式树(Expression Tree) 表达式树以树形数据结构表示代码,其中每一个节点都是一种表达式,比如

委托、泛型委托、多播委托 和 lamda表达式

委托基本概念:可以把一个方法作为一个参数传给另一个方法 声明:  方法前加 delegate  关键字 列子: using System; using System.Collections; using System.Collections.Generic; namespace Dome { class dom { static void Main(string[] args) { string[] sname = { "Abc", "dFg", "HjK