1.7个老师,每个人有3个学生,总共21一个学生里又有3个倒霉蛋没考及格,我们想要获得这3个倒霉蛋的集合
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class LINQ1 : MonoBehaviour
{
void Start()
{
FindStudent();
}
void FindStudent()
{
//7个老师,每个人有3个学生,总共21一个学生里又有3个倒霉蛋没考及格,我们想要获得这3个倒霉蛋的集合
List<Teacher> teachers = new List<Teacher>
{
new Teacher("a",new List<Student>{ new Student(100),new Student(90),new Student(30) }),
new Teacher("b",new List<Student>{ new Student(100),new Student(90),new Student(60) }),
new Teacher("c",new List<Student>{ new Student(100),new Student(90),new Student(40) }),
new Teacher("d",new List<Student>{ new Student(100),new Student(90),new Student(60) }),
new Teacher("e",new List<Student>{ new Student(100),new Student(90),new Student(50) }),
new Teacher("f",new List<Student>{ new Student(100),new Student(90),new Student(60) }),
new Teacher("g",new List<Student>{ new Student(100),new Student(90),new Student(60) })
};
//二重foreach循环
ForeachGet(teachers);
//查询表达式
LINQSelectGet(teachers);
//LINQSelectMany
LINQSelectManyGet(teachers);
//选出了门下有不及格学生的倒霉蛋老师+门生的分数
var list3 = teachers.SelectMany(
t => t.Students,
(t, s) => new { t.Name, s.Score })
.Where(n => n.Score < 60);
//给每个倒霉蛋再加10分
var list4 = teachers.SelectMany(t => t.Students).
Where(s => s.Score < 60).
Select(n => new Student(n.Score + 10));
}
void ForeachGet(List<Teacher> teachers)
{
List<Student> studentList = new List<Student>();
foreach (var t in teachers)
{
foreach (var s in t.Students)
{
if (s.Score < 60)
{
studentList.Add(s);
}
}
}
}
void LINQSelectGet(List<Teacher> teachers)
{
var list = from t in teachers
from s in t.Students
where s.Score < 60
select s;
List<Student> studentList = list.ToList();
}
void LINQSelectManyGet(List<Teacher> teachers)
{
var list = teachers.SelectMany(t => t.Students).Where(s => s.Score < 60);
List<Student> studentList = list.ToList();
}
}
public class Student
{
public int Score { get; set; }
public Student(int score)
{
this.Score = score;
}
}
public class Teacher
{
public string Name { get; set; }
public List<Student> Students;
public Teacher(string order, List<Student> students)
{
this.Name = order;
this.Students = students;
}
}
2.//从用户表customer和用户订单表orders中,查询年龄大于20,至少消费3笔满10块钱的用户的姓名和年龄。
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class LINQ2 : MonoBehaviour
{
//从用户表customer和用户订单表orders中,查询年龄大于20
//至少消费3笔满10块钱
//用户的姓名和年龄。
void Start()
{
List<Customer> customers = new List<Customer>();
List<Orders> orders = new List<Orders>();
#region select
var results = from customer in customers
//遍历customers年龄大于20的customer
where customer.age > 20
//找到对应ID的order,用需要得信息组成新的集合
//let子句可以引入一个变量,并对该变量赋值
let custOrders = (from order in orders
//遍历orders找到和当前customerID对应的
where customer.ID == order.ID
select new { order.num, order.amount })
where custOrders.Count(co => co.amount >= 10) >= 3
select new { customer.name, customer.age };
foreach (var result in results)
{
Debug.LogFormat("{0} {1}", result.name, result.age);
}
#endregion
}
}
/// <summary>
/// 用户表
/// </summary>
public class Customer
{
public int ID;
public string name;
public int age;
public List<Orders> orders;
}
/// <summary>
/// 用户订单表
/// </summary>
public class Orders
{
public int ID;
public float amount;//消费金额
public int num;//消费次数
}