• C# List LinQ Lambda 表达式


    ------------恢复内容开始------------

    # 参考链接 : https://blog.csdn.net/wori/article/details/113144580

    首先 => 翻译为{ }

    然后 Where 中为bool类型表达式
    再然后 Select 中为需要的形式: 如下面要返回Zoo,而不是string, 则 Select(e=>e)

    然后没有然后

    ### 主要基于我工作中常用的几种情况,写个小例子:

    这个Java叫实体类,C#不知道叫啥

     public class Zoo
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public string Type { get; set; }
            public int Location { get; set; }
            public float Money { get; set; }
    
            public Zoo(int iD, string name, string type, int location, float money)
            {
                ID = iD;
                Name = name;
                Type = type;
                Location = location;
                Money = money;
            }
        }
    

      然后测试几种情况:

    static void Main()
            {
    
                List<Zoo> lists = new List<Zoo>();
                Zoo z = new Zoo(001, "tiger", "Tiger", 21, 50);
                Zoo z1 = new Zoo(002, "tiger1", "Tiger", 21, 20);
                Zoo z2 = new Zoo(003, "tiger3", "Tiger", 21, 30);
                Zoo z3 = new Zoo(004, "tiger4", "Cat", 22, 40);
                Zoo z4 = new Zoo(005, "cat", "Cat", 22, 70);
                Zoo z5 = new Zoo(006, "lion", "BigStone", 20, 40);
                lists.Add(z);
                lists.Add(z1);
                lists.Add(z2);
                lists.Add(z3);
                lists.Add(z4);
                lists.Add(z5);
    
                //挑出其中的符合要求的:我这里写死要求为3、4、5
                List<Zoo> tigers = lists.FindAll(x =>x.ID>2&&x.ID<6);
                foreach (var item in tigers)
                {
                    Console.WriteLine(item.ID+"	"+item.Name);
                }
    
                //挑出符合要求且返回新集合,此处测试返回string列表
                //Where 中为bool类型表达式
                //Select 中为需要的形式: 如下面要返回Zoo,而不是string, 则 Select(e=>e)
                //最后的ToList是确定返回类型,根据实际需要
                List<string> zoos = lists.Where(x => (x.Money + 10) < 70).Select(e=>e.Name).ToList();
                foreach (var item in zoos)
                {
                    Console.WriteLine(item);
                }
                
                //返回单个符合要求的
                Console.WriteLine(lists.Find(x => x.Money==40 && x.Name.Contains("ger")).Type);
            }
    

      

    结果:

    3    tiger3
    4    tiger4
    5    cat
    tiger
    tiger1
    tiger3
    tiger4
    lion
    Cat

    https://www.cnblogs.com/dotnet261010/p/8278793.html

  • 相关阅读:
    linux下shell显示-bash-4.1#不显示路径解决方法
    update chnroute
    An error "Host key verification failed" when you connect to other computer by OSX SSH
    使用dig查询dns解析
    DNS被污染后
    TunnelBroker for EdgeRouter 后记
    mdadm详细使用手册
    关于尼康黄的原因
    Panda3d code in github
    Python实例浅谈之三Python与C/C++相互调用
  • 原文地址:https://www.cnblogs.com/FlyonGrass/p/14420345.html
Copyright © 2020-2023  润新知