Linq SelectMany 交叉连接
示例一
internal class Program { private static void Main(string[] args) { List<Teacher> teachers = new List<Teacher> { new Teacher("a",new List<Student1>{ new Student1(100),new Student1(90),new Student1(30) }), new Teacher("b",new List<Student1>{ new Student1(100),new Student1(90),new Student1(60) }), new Teacher("c",new List<Student1>{ new Student1(100),new Student1(90),new Student1(40) }), new Teacher("d",new List<Student1>{ new Student1(100),new Student1(90),new Student1(60) }), new Teacher("e",new List<Student1>{ new Student1(100),new Student1(90),new Student1(50) }), new Teacher("f",new List<Student1>{ new Student1(100),new Student1(90),new Student1(60) }), new Teacher("g",new List<Student1>{ new Student1(100),new Student1(90),new Student1(60) }) }; IEnumerable<Student1> t1 = teachers.SelectMany(s => s.Students.Where(w => w.Score < 60)); t1.ToList().ForEach(f => Console.WriteLine(f)); Console.Read(); } } internal class Student1 { public int Score { get; set; } public Student1(int score) { Score = score; } public override string ToString() { return $"studen1 : {Score}"; } } internal class Teacher { public string Name { get; set; } public List<Student1> Students; public Teacher(string order, List<Student1> students) { Name = order; Students = students; } }
示例二
var query = from left in Enumerable.Range(1, 4) from right in Enumerable.Range(11, left) select new { left, right }; var result = Enumerable.Range(1, 4).SelectMany(left => Enumerable.Range(11, left), (left, right) => new { left, right }); foreach (var item in result) { Console.WriteLine(item); }