LINQ 的Select、SelectMany

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;//消费次数
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值