无聊一下,感觉有点用记录一下,^_^ 用过linq的请绕道...
List<Test> list = new List<Test>(); list.Add(new Test { score = 10, name = "张君宝" }); list.Add(new Test { score = 20, name = "刘惜君" }); list.Add(new Test { score = 20, name = "刘惜君" }); list.Add(new Test { score = 30, name = "八戒" }); int total = (from temp in list where temp.name == "刘惜君" select temp.age).Count(); int score = (from temp in list where temp.name == "刘惜君" select temp.score).Sum(); this.textBox1.Text = "刘惜君:" + total + "人" + score + "score"; MessageBox.Show(this.textBox1.Text);
//return 刘惜君:2人40score
语法如下:
from 临时变量 in 实现IEnumerable<T>接口的对象
where条件表达式
[orderby 条件]
[group by 条件]
select 临时变量中被查询的值
以下内容来源于网络:
//1,使用LINQ做查询( 表达式写法)
var res = from m in masterList
//from后面设置查询的集合
where m.Level > 8 && m.Menpai == "丐帮"
//where后面跟上查询的条件
select m;//表示m的结果结合返回
LINQ 联合查询
var res = from m in masterList
from k in kongfuList
where m.Kongfu == k.Name && k.Power > 90
select m;
Join on集合联合查询
var res = from m in masterList
// join...in... 表示要连接的表,on后面为连接条件,等于要用equals,不能用==
join k in kongfuList on m.Kongfu equals k.Name
where k.Power > 90
select new { master = m, kongfu = k };
分组查询 into groups
把武林高手按照所学功夫分类,看一下那个功夫修炼的人数最多
var res = from k in kongfuList
join m in masterList on k.Name equals m.Kongfu
into groups //分组
orderby groups.Count() // 这个可以获得数量
select new { kongfu = k, count = groups.Count() };
来自:https://www.cnblogs.com/lmx282110xxx/p/10798682.html