UniRx第二季学习[linq与UniRx](一) ------ 复习总结/where/select

课程地址 : http://www.sikiedu.com/my/course/293

凉鞋大大的,可以的话大家多支持一波~


一.第一季总结 

UniRx第二季学习[linq与UniRx](一) ------ 复习总结/where/select_第1张图片

 二.Where

功能 : 过滤作⽤

UniRx第二季学习[linq与UniRx](一) ------ 复习总结/where/select_第2张图片

1.Linq

public class LINQWhereQueryExample : MonoBehaviour
{
   class Student
   {
      public string Name;
      public int Age;
   }
   // Use this for initialization6
   void Start()
   {
      var students = new List()
      {
         new Student() {Name = "张三", Age = 50},
         new Student() {Name = "张三", Age = 50},
         new Student() {Name = "李四", Age = 40}
      };
   //查询式写法
   var oldStudents = from oldStudent in students where oldStudent.Age > 45 select oldStudent;
  //链式写法
  var oldStudents = students.Where(student => student.Age > 45); 
   foreach (var student in oldStudents)
   {
      Debug.Log(student.Name);
   }
}

 2.UniRx

 void Start()
 {
            //查询式写法
            (from updateEvent in Observable.EveryUpdate()
             where Input.GetMouseButtonDown(0)
             select updateEvent)
                .Subscribe(_ =>
                {
                    Debug.Log("mouse down");
                }).AddTo(this);
             
            //链式写法
             Observable.EveryUpdate() 
              .Where(_ => Input.GetMouseButtonDown(0)) 
              .Subscribe(_ => { Debug.Log("mouse down"); }) 
              .AddTo(this); 
 }

三.Select

介绍 Where 操作符的时候,不管是 UniRx 还是 LINQ,其查询表达式都多出了⼀个select。但是在链式的写法中,却没有使⽤其对应的 Select 操作符, 查询式写法中,编译器要求必须要在最后 select ⼀下。⽽链式写法,我们可以理解成省略掉了。(其实就是选择了⼀下⾃⼰。除了选择⾃⼰,其实还可以选择⼀个其他类型的变量作为结果。)

功能 : 查询表达式中的 select 及所接⼦句是放在最后并把⼦句中的变量也就是结果返回回来。

UniRx第二季学习[linq与UniRx](一) ------ 复习总结/where/select_第3张图片

实现当每次抬起⿏标左键,则输出 mouse up : 

1.linq

class Student
        {
            public string Name;

            public int Age;
        }

        void Start()
        {
            var students = new List()
            {
                new Student { Name = "张三", Age = 18 },
                new Student { Name = "张三", Age = 18 },
                new Student { Name = "李四", Age = 10 },
            };
            
            //链式写法
            students.Where(student => student.Age > 10)
            .Select(student => student.Name)
            .ToList()
            .ForEach(studentName =>
            {
                Debug.Log(studentName);
            });
           
            //查询式写法
            (from student in students where student.Age > 10 select student.Name)
                     .ToList()
                     .ForEach(studentName =>
                     {
                         Debug.Log(studentName);
                     });
        }

2.UniRx

 void Start()
        {
            //链式写法
            Observable.EveryUpdate()
            .Where(_ => Input.GetMouseButtonDown(0))
            .Select(_ => "mouse down")
            .Subscribe(mouseEventName =>
            {
                Debug.Log(mouseEventName);
            })
            .AddTo(this);
            
            //查询式写法
            (from updateEvent in Observable.EveryUpdate() where Input.GetMouseButtonDown(0) select "mouse down")
                .Subscribe(mouseEventName => Debug.Log(mouseEventName))
                .AddTo(this);
        }

 

你可能感兴趣的:(框架)