• 关于List筛选数据的笔记


    List有集成了很多方法,如果在一个list中,需要选择仅仅需要的字段,或者筛选出满足条件的对象,可以参考此种用法:

    namespace TestDemo
    {
        public class Program
        {
            static void Main(string[] args)
            {
                List<Person> perList = new List<Person>() { 
                    new Person(){Id=1,Name="xqq",Age=27,Description="This is a Test"},
                    new Person(){Id=1,Name="hjj",Age=26,Description="This is a Test"},
                    new Person(){Id=1,Name="zcy",Age=26,Description="This is a Test"},
                    new Person(){Id=1,Name="lxq",Age=25,Description="This is a Test"}
                };
                //List Select仅仅是选中此List的相关属性(name,age...)
                perList.Select(p => p.Name).ToList().ForEach(r => Console.WriteLine(r));
                //List Select选择多个属性
                perList.Select(p => new { p.Name, p.Age }).ToList().ForEach(r=>Console.WriteLine(r));
                //List 选择name=xqq的对象的(name,age及description属性)
                perList.Select(p => new { p.Name, p.Age, p.Description }).Where(p => p.Name == "xqq").ToList().ForEach(r=>Console.WriteLine(r));
    
                //查询的返回第一条记录
                Console.WriteLine(perList.Select(p => new { p.Name }).ToList().Find(d => d.Name.Contains("q")).ToString());
                //查询返回所有的记录
                perList.Select(p => new { p.Name, p.Age }).ToList().FindAll(d => d.Name.Contains("q")).ForEach(r=>Console.WriteLine(r));
                Console.ReadKey(true);
            }
    
    
        }
    
        public class Person
        {
            public int Id { get; set; }
    
            public string Name { get; set; }
    
            public int Age { get; set; }
    
            public string Description { get; set; }        
        }
    }

    仅自己参考

  • 相关阅读:
    QT之QRect函数QRect::adjust()函数
    QT 正则表达式(进阶篇)IP,端口号,文件名,非空格字符的匹配,已验证
    QT 正则表达式(基础篇)
    处理不平衡数据的策略
    记录一下ssh,nfs安装步骤
    用户偏好的回归预测推荐
    SVD++分解
    BiasLFM分解
    WALS分解
    ALS分解
  • 原文地址:https://www.cnblogs.com/yuhuabaobao/p/7019362.html
Copyright © 2020-2023  润新知