LINQ 学习路程 -- 查询操作 Select, SelectMany
2024-10-21 15:39:23
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John" },
new Student() { StudentID = , StudentName = "Moin" },
new Student() { StudentID = , StudentName = "Bill" },
new Student() { StudentID = , StudentName = "Ram" },
new Student() { StudentID = , StudentName = "Ron" }
}; var selectResult = from s in studentList
select s.StudentName;
select可以定义结果数据类型,可以返回用户自定义的类型或者匿名类型
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Moin", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; // returns collection of anonymous objects with Name and Age property
var selectResult = from s in studentList
select new { Name = "Mr. " + s.StudentName, Age = s.Age }; // iterate selectResult
foreach (var item in selectResult)
Console.WriteLine("Student Name: {0}, Age: {1}", item.Name, item.Age);
方法查询
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "John", Age = } ,
new Student() { StudentID = , StudentName = "Moin", Age = } ,
new Student() { StudentID = , StudentName = "Bill", Age = } ,
new Student() { StudentID = , StudentName = "Ram" , Age = } ,
new Student() { StudentID = , StudentName = "Ron" , Age = }
}; var selectResult = studentList.Select(s => new { Name = s.StudentName ,
Age = s.Age });
SelectMany
class Student
{
public int Score { get; set; } public Student(int score)
{
this.Score = score;
}
} class Teacher
{
public string Name { get; set; } public List<Student> Students; public Teacher(string order,List<Student> students)
{
this.Name = order; this.Students = students;
}
}
List<Teacher> teachers = new List<Teacher>
{
new Teacher("a",new List<Student>{ new Student(),new Student(),new Student() }),
new Teacher("b",new List<Student>{ new Student(),new Student(),new Student() }),
new Teacher("c",new List<Student>{ new Student(),new Student(),new Student() }),
new Teacher("d",new List<Student>{ new Student(),new Student(),new Student() }),
new Teacher("e",new List<Student>{ new Student(),new Student(),new Student() }),
new Teacher("f",new List<Student>{ new Student(),new Student(),new Student() }),
new Teacher("g",new List<Student>{ new Student(),new Student(),new Student() })
};
这里有7个老师,每个人有3个学生,总共21一个学生里 查询3个倒霉蛋没考及格
var list1 = from t in teachers
from s in t.Students
where s.Score <
select s;
var list2 = teachers.SelectMany(t => t.Students).Where(s => s.Score < );
public static IEnumerable<TResult> SelectMany<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, IEnumerable<TResult>> selector
) public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
this IEnumerable<TSource> source,
Func<TSource, IEnumerable<TCollection>> collectionSelector,
Func<TSource, TCollection, TResult> resultSelector
)
选出了门下有不及格学生的倒霉蛋老师+门生的分数
var list3 = teachers.SelectMany(
t => t.Students,
(t, s) => new { t.Name, s.Score })
.Where(n => n.Score < );
最新文章
- nginx的使用
- 剑指Offer面试题:7.旋转数组的最小数字
- 使用http.sys,让delphi 的多层服务飞起来
- 使用CSS3线性渐变实现图片闪光划过效果
- JAVA - HashMap和HashTable
- f2fs中node page的lock_page
- 同级兄弟元素之间的CSS控制
- [译]PrestaShop开发者指南 第一篇 基础
- c++中的 extern ";C";(转载)
- 成为一个PHP专家:缺失的环节
- NTP-ntpdate:no server suitable for synchronization found
- 深入浅出学习Spring框架(四):IoC和AOP的应用——事务配置
- PowerMockito使用详解
- 设置position(absolute,fixed)导致flex布局不生效
- Think twice before starting the adventure
- Nice Garland CodeForces - 1108C (思维+暴力)
- MTALAB——神经网络mae()、mse()、sse()
- Eclipse ADT中的logcat不显示解决方法
- 【LeetCode】Longest Substring with At Most Two Distinct Characters (2 solutions)
- 洛谷P3265 [JLOI2015]装备购买 [线性基]