linq 用法和实践
一 linq 用法
方法语法和查询语法
在使用LINQ写查询时可以使用两种形式的语法:查询语法和方法语法
a:方法语法(method syntax) : 使用标准的方法调用,这些方法是一组叫做标准查询运算符的方法
b:查询语法(query method) : 看上去和SQL语句很相似,使用查询表达式形式书写。微软推荐使用查询语法,因为它更易读
在编译时,CLR会将查询语法转换为方法语法
int[] num = { 2, 4, 6, 8, 10 };
var numQuery = from number in num //查询语法
where number < 8
select number;
var numMethod = num.Where(x => x < 8); //方法语法
它们得到的结果是一样的。方法语法中where的参数使用了Lambda表达式
https://blog.csdn.net/qq_38916538/article/details/107359698
https://www.cnblogs.com/liqingwen/p/5801249.html
https://www.cnblogs.com/liqingwen/p/5832322.htmls
https://blog.csdn.net/zlbcdn/article/details/79192485
https://blog.csdn.net/lym940928/article/details/80278783
这篇文章很好
https://www.nhooo.com/linq/linq-lambda-expression.html
二实践
public class Student
{
public int stId { get; set; }
public string stuName { get; set; }
public int num;
}
public class Course
{
public int stId; //学生ID
public string courseName; //课程名
public int chengji;
public int num2;
}
var query2 = from s in students
join c in courses on s.stId equals c.stId
select new { s.stId, s.stuName, c.courseName, c.chengji };
实例一
var query3 = query2.Select(x=>x.courseName);
foreach (var item in query3)
{
Console.WriteLine(item);
// //输出
// //art
// //history
// //art
// //history
// //physics
// //art
// //history
// //art
// //history
// //physics
}
实例二
foreach (var item in query2)
{
Console.WriteLine(item);
}
输出
{ stId = 1, stuName = jack, courseName = art, chengji = 80 }
{ stId = 1, stuName = jack, courseName = history, chengji = 70 }
{ stId = 2, stuName = taylor, courseName = art, chengji = 88 }
{ stId = 3, stuName = fleming, courseName = history, chengji = 60 }
{ stId = 3, stuName = fleming, courseName = physics, chengji = 100 }
实例三
var query4 = query2.GroupBy(x => x.courseName, (index, item) =>
{ //在这里 index取值是 x.courseName
return item;//.Where(m => m.chengji > 81);
}).ToList();
foreach (var item in query4)
{
Console.WriteLine(item.FirstOrDefault()+" ======ddd");
}
输出
{ stId = 1, stuName = jack, courseName = art, chengji = 80 } ======ddd
{ stId = 1, stuName = jack, courseName = history, chengji = 70 } ======ddd
{ stId = 3, stuName = fleming, courseName = physics, chengji = 100 } ======ddd
实例四
var query5 = query2.GroupBy(x => x.courseName, (indexGroupKey, item) =>
{ //indexGroupKey 取值是 x.courseName
// return new { indexGroupKey, newData= item.FirstOrDefault()}; 这也是一种方法
var newItem = item.FirstOrDefault();//取第一项
return new { fenzuziduan= indexGroupKey, stId= newItem.stId, courseName=newItem.courseName, stuName=newItem.stuName,sumData=item.Sum(fen=>fen.chengji) };
}).ToList();
foreach (var item in query5)
{
Console.WriteLine(item);
}
输出
{ fenzuziduan = art, stId = 1, courseName = art, stuName = jack, sumData = 168 }
{ fenzuziduan = history, stId = 1, courseName = history, stuName = jack, sumData = 130 }
{ fenzuziduan = physics, stId = 3, courseName = physics, stuName = fleming, sumData = 100 }
运行结果图
public class StudentBig
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
}
IList<StudentBig> studentList = new List<StudentBig>() {
new StudentBig() { StudentID = 1, StudentName = "John", Age = 18 } ,
new StudentBig() { StudentID = 2, StudentName = "Steve", Age = 21 } ,
new StudentBig() { StudentID = 3, StudentName = "Bill", Age = 18 } ,
new StudentBig() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
new StudentBig() { StudentID = 5, StudentName = "Abram" , Age = 21 }
};
var groupedResult = studentList.GroupBy(s => s.Age);
foreach (var ageGroup in groupedResult)
{
Console.WriteLine("Age Group: {0}", ageGroup.Key); //每组都有一个钥匙
foreach (StudentBig s in ageGroup) //每个组都有一个内部集合
Console.WriteLine("Student Name: {0}", s.StudentName);
}