• LINQ 操作符(二)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Lambda表达式
    {
        class Program
        {
            static void Main(string[] args)
            {
    
            }
            static List<Person> GetPersonList()
            {
                return new List<Person>() { 
                new Person (){ Age =1,Name ="小白01",ID=1,Gender =true},
                new Person (){Age =2,Name ="小白02",ID=2,Gender =false},
                new Person (){Age =3,Name ="小白03",ID=3,Gender =true}
                };
            }
    
            static List<PersonToy> GetPersonToyList()
            {
                return new List<PersonToy>() { 
                new PersonToy(){ PersonToyID=1,Name="小明"},
                new PersonToy (){PersonToyID =2,Name ="小白"},
                new PersonToy (){PersonToyID =3,Name ="小红"},
                new PersonToy (){PersonToyID =4,Name ="小黑"}
                };
            }
    
            #region Lambda表达式
            /// <summary>
            /// Lambda表达式和Lambda语句
            /// </summary>
            static void TestLambda()
            {
                List<Person> list = GetPersonList();
                list.ForEach(d => Console.WriteLine(d.Name));
                //Lambda表达式
                list.FindAll(d => d.Age > 1);
                list.Sort((x, y) => x.Age - y.Age);
                var it = list.Select(p => new smallperson { Name = p.Name });
                //语句Lambda语句
                var it1 = list.Select(p =>
                { //如果存在多个语句就不能省略{}大括号 也不能省略return
                    smallperson sp = new smallperson();
                    sp.Name = p.Name;
                    return sp;
                });
            }
            #endregion
    
            /// <summary>
            /// 标准查询方法 ----Where查询
            /// </summary>
            static void SQOWhere()
            {
                List<Person> list = GetPersonList();
                //在集合中筛选出符合条件的数据
                List<Person> list2 = list.Where(p => p.Gender == false && p.Age > 1 && p.Age < 3).ToList();
                list.ForEach(p => Console.WriteLine(p.Name));
            }
    
            /// <summary>
            /// 标准查询运算符 select 投射方法(返回一个新的集合)
            /// </summary>
            static void SQOSelect()
            {
                List<Person> list = GetPersonList();
                List<smallperson> list2 = list.Where(p => p.Gender == false).Select(p => new smallperson() { Name = p.Name }).ToList();
            }
    
            /// <summary>
            /// 标准查询运算符 Order 排序方法 返回一个新的集合
            /// </summary>
            static void SQOOrder()
            {
                List<Person> list = GetPersonList();
                List<Person> listSorted = list.OrderBy(p => p.Age).ThenByDescending(p => p.ID).ToList();//按照年龄进行排序(升序)
                // list .OrderByDescending 倒序
    
                list.ForEach(p => Console.WriteLine(p.Age.ToString()));
            }
    
            /// <summary>
            /// 标准运算符--连接集合
            /// </summary>
            static void TestJoin()
            {
                List<Person> listperson = GetPersonList();
                List<PersonToy> listPersontToy = GetPersonToyList();
                var listJoin = listperson.Join(listPersontToy, d => d.ID, t => t.PersonToyID, (d, t) => new { ID = d.ID, ToyName = t.Name, Name = d.Name }).ToList();
    
                listJoin.ForEach(a => Console.WriteLine(a.ID.ToString() + "," + a.ToyName));
            }
    
            /// <summary>
            /// 标准查询运算符 GroupBy为集合分组
            /// 
            /// </summary>
            static void SQOGroupBy()
            {
                List<Person> list = GetPersonList();
                //按照性别进行分组
                IEnumerable<IGrouping<bool, Person>> it = list.GroupBy(p => p.Gender);
    
                //根据姓名进行分组
                IEnumerable<IGrouping<string, Person>> it2 = list.GroupBy(p => p.Name);
                // var it2= list.GroupBy(p=>p.Name );
    
                IList<IGrouping<bool, Person>> listGroup = it.ToList();
                foreach (var group in listGroup)
                {
                    Console.WriteLine(group.Key.ToString());
                    foreach (var item in group)
                    {
                        Console.WriteLine(item.Name + "," + item.Age);
                    }
                    Console.WriteLine("------------------------------------");
                }
            }
    
            /// <summary>
            /// 标准运算符=Skip -Take分页数据
            /// </summary>
            static void SQOPagedList()
            {
                List<Person> list = GetPersonList();
            }
    
            /// <summary>
            ///Linq语句 ,编译后会生成对应的标准查询 运算符 linq是一种类似sql的一种更加友好的语法糖 语法而已
            /// </summary>
            static void TestLinq()
            {
                List<Person> list = GetPersonList();
                List<PersonToy> listtoy = GetPersonToyList();
                var query = from li in list select li;//从老集合中每一个元素 存入一个新的集合
                //where
                var query1 = from li in list where li.Gender == true select li;
                //order descending倒序 aescending正序
                var query2 = from li in list orderby li.Gender orderby li.Age descending select li;
                //join链接查询
                var query3 = from li in list join t in listtoy on li.ID equals t.PersonToyID select new { Name = li.Name };
                //分组查询
                var query4 = from li in list group li by li.Gender;
    
            }
    
        }
        public class smallperson
        {
            public string Name { get; set; }
        }
    }
  • 相关阅读:
    Sereja and Bottles-水题有点坑爹
    what we benefit from big data
    SwipeRefreshLayout与ViewPager滑动事件冲突解决
    java连接数据库核心代码
    spring mvc中实现csrf安全防御简记
    一题多解(八)—— 矩阵上三角(下三角)的访问
    面向对象举例(一) —— 顶点(vertex)、边(edge)与图(graph)
    面向对象举例(一) —— 顶点(vertex)、边(edge)与图(graph)
    Python 辨异 —— __init__ 与 __new__
    Python 辨异 —— __init__ 与 __new__
  • 原文地址:https://www.cnblogs.com/sumg/p/3743544.html
Copyright © 2020-2023  润新知