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);
posted @ 2018-09-28 10:09  谁说程序猿很猥琐  阅读(3026)  评论(0编辑  收藏  举报