• Linq To DataTable


    private void button1_Click(object sender, EventArgs e)
    
    {
    
    DataTable dt = new
    DataTable();
    
    dt.Columns.Add("Name", typeof(string));
    
    dt.Columns.Add("Age", typeof(int));
    
    dt.Columns.Add("Sex", typeof(string));
    
    dt.Rows.Add("z", 10, "male");
    
    dt.Rows.Add("z", 10, "male");
    
    dt.Rows.Add("x", 11, "male");
    
    dt.Rows.Add("y", 12, "male");
    
    //查询Name=‘z’的所有行
    
    var rows = dt.Select("Name='z'");
    
    
    
    //使用linq进行各种查询
    
    var q1 = (from d in dt.AsEnumerable()
    
    where d.Field("Age") > 10
    
    orderby d.Field("Name") descending
    
    select new Item //创建一个类,用于存储属性
    
    {
    
    Age = d.Field("Age"),
    
    Name = d.Field("Name")
    
    }
    
    );
    
    
    
    foreach (Item t
    in q1.ToList())
    
    {
    
    MessageBox.Show(t.Name + t.Age
    + t.Sex);
    
    }
    
    var q = q1.Where(p => p.Age
    > 10).ToList();//筛选年龄大于10的信息
    
    
    
    //获取datatable的总数
    
    string rowCount = dt.Compute("count(Name)", null).ToString();
    
    
    
    //计算某一列的值
    
    string sumCount = dt.Compute("sum(Age)", null).ToString();
    
    
    
    //取得某一列的平均值
    
    string avgValue = dt.Compute("avg(Age)", null).ToString();
    
    
    
    //取得最小值
    
    string minValue = dt.Compute("min(Age)", null).ToString();//max最大值
    
    
    
    //------------------多条件排序
    
    var list = from d in dt.AsEnumerable()
    
    orderby d.Field("Age") descending, d.Field("Name") ascending//
    new { t1 = d.Field("Age") , t2 = d.Field("Name") }
    
    select new
    
    {
    
    
    
    Name = d.Field("Name"),
    
    Age = d.Field("Age")
    
    };
    
    //--------------分组
    
    //按照Name分组,并且将Name对应的Age的值得和统计
    
    var group = from d in dt.AsEnumerable()
    
    group d by
    d.Field("Name") into m
    
    select new
    
    {
    
    Name = m.Key,//Name
    
    sum = m.Sum(n => n.Field("Age")),//
    
    avg=m.Average(n=>n.Field("Age")),//平均值
    
    max=m.Max(n => n.Field("Age")),//最大值
    
    min=m.Min(n => n.Field("Age")),//最小值
    
    count=m.Count()//个数
    
    
    
    };
    
    foreach (var m in group.ToList())
    
    MessageBox.Show(m.Name + "," + m.sum
    +","+m.count);
    private void button1_Click(object sender, EventArgs e)
    {
    DataTable dt = new DataTable();
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("Age", typeof(int));
    dt.Columns.Add("Sex", typeof(string));
    dt.Rows.Add("z", 10, "male");
    dt.Rows.Add("z", 10, "male");
    dt.Rows.Add("x", 11, "male");
    dt.Rows.Add("y", 12, "male");
    //查询Name=‘z’的所有行
    var rows = dt.Select("Name='z'");
    //使用linq进行各种查询
    var q1 = (from d in dt.AsEnumerable()
    where d.Field("Age") > 10
    orderby d.Field("Name") descending
    select new Item //创建一个类,用于存储属性
    {
    Age = d.Field("Age"),
    Name = d.Field("Name")
    }
    );
    foreach (Item t in q1.ToList())
    {
    MessageBox.Show(t.Name + t.Age + t.Sex);
    }
    var q = q1.Where(p => p.Age > 10).ToList();//筛选年龄大于10的信息
    //获取datatable的总数
    string rowCount = dt.Compute("count(Name)", null).ToString();
    //计算某一列的值
    string sumCount = dt.Compute("sum(Age)", null).ToString();
    //取得某一列的平均值
    string avgValue = dt.Compute("avg(Age)", null).ToString();
    //取得最小值
    string minValue = dt.Compute("min(Age)", null).ToString();//max最大值
    //------------------多条件排序
    var list = from d in dt.AsEnumerable()
    orderby d.Field("Age") descending, d.Field("Name") ascending// new { t1 = d.Field("Age") , t2 = d.Field("Name") }
    select new
    {
    Name = d.Field("Name"),
    Age = d.Field("Age")
    };
    //--------------分组
    //按照Name分组,并且将Name对应的Age的值得和统计
    var group = from d in dt.AsEnumerable()
    group d by d.Field("Name") into m
    select new
    {
    Name = m.Key,//Name
    sum = m.Sum(n => n.Field("Age")),//和
    avg=m.Average(n=>n.Field("Age")),//平均值
    max=m.Max(n => n.Field("Age")),//最大值
    min=m.Min(n => n.Field("Age")),//最小值
    count=m.Count()//个数
    };
    foreach (var m in group.ToList())
    MessageBox.Show(m.Name + "," + m.sum +","+m.count);
  • 相关阅读:
    CoreAnimation6-基于定时器的动画和性能调优
    CoreAnimation5-图层时间和缓冲
    CoreAnimation4-隐式动画和显式动画
    CoreAnimation3-专用图层
    CoreAnimation2-视觉效果和变换
    CoreAnimation1-图层树、寄宿图以及图层几何学
    Quartz-2D绘图之路径(Paths)详解
    Quartz-2D绘图之图形上下文详解
    Objective-C设计模式——抽象工厂模式Abstract Factory(对象创建)
    Objective-C设计模式——工厂方法模式virtual constructor(对象创建)
  • 原文地址:https://www.cnblogs.com/hnzheng/p/9717084.html
Copyright © 2020-2023  润新知