LINQ学习:Select的⽤法
说明:在查询表达式中,select ⼦句可以指定将在执⾏查询时产⽣的值的类型。该⼦句的结果将基于前⾯所有⼦句的计算结果以及 select ⼦句本⾝中的所有表达式。查询表达式必须以 select ⼦句或 group ⼦句结束。
Select操作包括7种形式,分别为简单⽤法、匿名类型形式、条件形式、筛选形式、嵌套类型形式、本地⽅法调⽤形式、Distinct形式。下⾯分别⽤实例举例下:
class Student
{
public string Name { get; set; }
public int Score { get; set; }
}
List<Student> students = new List<Student>{
new Student {Name="Terry", Score=50},
new Student {Name="AI", Score=80},
new Student {Name="AI", Score=70},
};
1.简单⽤法
说明:当以select结尾时表⽰的只是⼀个声明或者⼀个描述,并没有真正把数据取出来,只有当你需要该数据的时候,它才会执⾏这个语句,这就是延迟加载(deferred loading)。
查询学⽣的姓名:
var query = from student in students
select student.Name;
foreach (var student in query)
{
Console.WriteLine("{0}", student);
//Terry
//AI
//AI
}
2.匿名类型形式
说明:其实质是编译器根据我们⾃定义产⽣⼀个匿名的类来帮助我们实现临时变量的储存。例如 var ob = new {Name = "Harry"},编译器⾃动产⽣⼀个有property叫做Name的匿名类,然后按这个类型分配内存,并初始化对象。
查询学⽣的姓名:
var query = from student in students
select new
{
newName = "学⽣姓名:" + student.Name
};
foreach (var student in query)
{
Console.wName);
//学⽣姓名:Terry
//学⽣姓名:AI
//学⽣姓名:AI
}
3.条件形式
说明:三元运算,类似于SQL语句case when condition then else的⽤法。
查询学⽣的分数等级:
var query = from student in students
select new
{
student.Name,
level = student.Score < 60 ? "不及格" : "合格"
};
foreach (var student in query)
{
Console.WriteLine("{0}:{1}", student.Name, student.level);
4.筛选形式
说明:结合where⽤起到过滤的作⽤。
查询Terry的分数:
var query = from student in students
where student.Name == "Terry"
select student;
foreach (var student in query)
{
Console.WriteLine("{0}:{1}",student.Name,student.Score);
//Terry:50
}
5.嵌套类型形式
说明:如果⼀个数据源⾥⾯⼜包含了⼀个或多个集合列表,那么应该使⽤复合的select⼦句来进⾏查询。查询⼤于80分的学⽣分数:
class Student
{
public string Name { get; set; }
public List<int> Scores { get; set; }
}
List<Student> students = new List<Student>{
new Student {Name="Terry", Scores=new List<int> {97, 72, 81, 60}},
new Student {Name="AI", Scores=new List<int> {75, 84, 91, 39}},
new Student {Name="Wade", Scores=new List<int> {88, 94, 65, 85}},
new Student {Name="Tracy", Scores=new List<int>{97, 89, 85, 82}},
new Student {Name="Kobe", Scores=new List<int> {35, 72, 91, 70}}
};
var query = from student in students
select new
{select中distinct
student.Name,
//⽣成新的集合对象
highScore=from sc in student.Scores
where sc>80
select sc
};
foreach (var student in query)
{
Console.Write("{0}:",student.Name);
foreach (var scores in student.highScore)
{
Console.Write("{0},",scores);
}
Console.WriteLine();
//Terry:97,81,
//AI:84,91,
/
/Wade:88,94,85,
//Tracy:97,89,85,82,
//Kobe:91,
}
6.本地⽅法调⽤形式
说明:调⽤⾃定义⽅法。
var query = from student in students
select new
{
student.Name,
//调⽤GetLevel⽅法
level = GetLevel(student.Score)
};
foreach (var student in query)
{
Console.WriteLine("{0}:{1}", student.Name, student.level);
protected static string GetLevel(int score)
{
if (score > 60)
{
return "及格";
}
else
{
return "不及格";
}
}
7.Distinct形式
说明:⽤于查询不重复的结果集。类似于SQL语句SELECT DISTINCT 。查询不重复的学⽣姓名:
var query = (from student in students
select student.Name).Distinct();
foreach (var student in query)
{
Console.WriteLine("{0}", student);
//Terry:
//AI
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论