Linq语句初接触

在unity中简单的使用Linq语句

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;


public struct UserInfo
{
    public string name;
    public string id;
    public int wage;


    public UserInfo(string _name,string _id,int _index)
    {
        name = _name;
        id = _id;
        wage = _index;
    }
}

public class LinqTest : MonoBehaviour
{
    List userList = new List();

    // Start is called before the first frame update
    void Start()
    {
        UserInfo info = new UserInfo("张三五", "1",0);
        userList.Add(info);

        info = new UserInfo("李四六", "456",3);
        userList.Add(info);

        info = new UserInfo("王五", "23",1);
        userList.Add(info);

        info = new UserInfo("赵六", "7890",2);
        userList.Add(info);
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Alpha0))
        {
            Debug.Log("名字包含关键字==============================================================");
            UserInfo[] info = (from item in userList where item.name.Contains("五") select item).ToArray();
            AllUserInfo(new List(info));
        }

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            Debug.Log("ID查询==============================================================");
            UserInfo[] info = (from item in userList where item.id == "1" select item).ToArray();
            AllUserInfo(new List(info));
        }

        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            Debug.Log("跳过前两个元素==============================================================");
            UserInfo[] info = (from item in userList select item).Skip(2).ToArray();
            AllUserInfo(new List(info));
        }

        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            Debug.Log("倒排序==============================================================");
            UserInfo[] info = (from item in userList  orderby item.id.Length descending select item).ToArray();
            AllUserInfo(new List(info));
        }

        if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            Debug.Log("正排序==============================================================");
            UserInfo[] info = (from item in userList orderby item.id.Length ascending select item).ToArray();
            AllUserInfo(new List(info));
        }

        if (Input.GetKeyDown(KeyCode.Alpha5))
        {
            Debug.Log("选取指定个数元素==============================================================");
            UserInfo[] info = (from item in userList select item).Take(3).ToArray();
            AllUserInfo(new List(info));
        }

        if (Input.GetKeyDown(KeyCode.Alpha6))
        {
            Debug.Log("分组==============================================================");
            var info = (from item in userList group item by item.id.Length > 2 into g select g).ToArray();
            foreach(var g in info)
            {
                Debug.Log($"Key:{g.Key}");
                foreach(var iteam in g)
                {
                    Debug.Log($"name:{iteam.name},ID:{iteam.id},Index:{iteam.wage}");
                    
                }
            }
        }

        if (Input.GetKeyDown(KeyCode.Alpha7))
        {
            Debug.Log("选择第一个==============================================================");
            UserInfo info = (from item in userList select item).FirstOrDefault();
            Debug.Log($"name:{info.name},ID:{info.id},Index:{info.wage}");
        }

        if (Input.GetKeyDown(KeyCode.Alpha8))
        {
            Debug.Log("简单计算==============================================================");
            int info = (from item in userList select item).Sum(x=>x.wage);
            Debug.Log($"Sum方法计算所有人工资总和:{info}");

            info = (from item in userList select item).Count();
            Debug.Log($"Count方法计算工人数:{info}");

            info = (from item in userList select item).Max(x=>x.wage);
            Debug.Log($"Max方法获取最高工资:{info}");

            info = (from item in userList select item).Min(x => x.wage);
            Debug.Log($"Min方法获取最低工资:{info}");
        }
    }

    void AllUserInfo(List list)
    {
        foreach(UserInfo user in list)
        {
            Debug.Log($"name:{user.name},ID:{user.id},Index:{user.wage}");
        }
    }
}

你可能感兴趣的:(Linq语句初接触)