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 < );

最新文章

  1. nginx的使用
  2. 剑指Offer面试题:7.旋转数组的最小数字
  3. 使用http.sys,让delphi 的多层服务飞起来
  4. 使用CSS3线性渐变实现图片闪光划过效果
  5. JAVA - HashMap和HashTable
  6. f2fs中node page的lock_page
  7. 同级兄弟元素之间的CSS控制
  8. [译]PrestaShop开发者指南 第一篇 基础
  9. c++中的 extern &quot;C&quot;(转载)
  10. 成为一个PHP专家:缺失的环节
  11. NTP-ntpdate:no server suitable for synchronization found
  12. 深入浅出学习Spring框架(四):IoC和AOP的应用——事务配置
  13. PowerMockito使用详解
  14. 设置position(absolute,fixed)导致flex布局不生效
  15. Think twice before starting the adventure
  16. Nice Garland CodeForces - 1108C (思维+暴力)
  17. MTALAB——神经网络mae()、mse()、sse()
  18. Eclipse ADT中的logcat不显示解决方法
  19. 【LeetCode】Longest Substring with At Most Two Distinct Characters (2 solutions)
  20. 洛谷P3265 [JLOI2015]装备购买 [线性基]

热门文章

  1. 篇三、开发前知识补充:Android的长度单位和屏幕分辨率,这个也是转载~~
  2. Ocelot --API网关简单使用
  3. 【ubantu】在ubantu下如何对svn进行操作命令
  4. 【POJ-2524】Ubiquitous Religions(并查集)
  5. linux php.ini又一次载入问题
  6. javascript修改图片链接地址
  7. 使用tomcat7-maven-plugin部署Web项目
  8. iOS Load方法 和 initialize方法的比较
  9. Python菜鸟之路:JQuery基础
  10. android 中使用svg