C# LINQ语法
from子句嵌套
private void button5_Click(object sender, EventArgs e) { listBox1.Items.Clear(); List<Student1> students = new List<Student1> { new Student1{Name = "张三",Scores = new List<int>{93,74,94,58}}, new Student1{Name = "李四",Scores = new List<int>{94,74,86,58}}, new Student1{Name = "王五",Scores = new List<int>{95,74,86,58}}, new Student1{Name = "赵六",Scores = new List<int>{96,74,86,58}}, new Student1{Name = "冯七",Scores = new List<int>{97,74,86,58}} }; var scoreQuery = from student in students from scores in student.Scores where scores > 90 select new { name = student.Name, scores }; foreach (var stu in scoreQuery) { listBox1.Items.Add(string.Format("{0}超过90分的成绩有{1}",stu.name,stu.scores)); } }
Form子句交叉连接查询
private void button6_Click(object sender, EventArgs e) { char[] upperCase = { 'A', 'B', 'C' }; char[] lowerCase = { 'x', 'y', 'z' }; var joinQuery1 = from upper in upperCase from lower in lowerCase select new { upper, lower }; var joinQuery2 = from lower in lowerCase where lower != 'x' from upper in upperCase select new { upper, lower }; foreach (var pair in joinQuery1) { listBox1.Items.Add(string.Format("{0}匹配{1}",pair.upper,pair.lower)); } listBox1.Items.Add("----我是无聊的分界线----"); foreach (var pair in joinQuery2) { listBox1.Items.Add(string.Format("{0}匹配{1}", pair.lower, pair.upper)); } }
select语句投影
使用select子句将数据源转换为新类型的序列
group分组
group *** by ****;
into 关键字
group关键字的同时配合使用into关键字。
let子句
let子句可以将表达式(如方法调用)的结果存储到新的范围变量中
var xxx = from x in xxx let sum_x = x.Scores[0] + x.Scores[1] + x.Scores[2] where sum_x/3 > 80 select string.Format("平均值大于80的有{0},平均分为{1}",x.name,sum_x/4);
文章对您有帮助,开心可以打赏我,金额随意,欢迎来赏!
需要电子方面开发板/传感器/模块等硬件可以到我的淘宝店逛逛