C# LINQ简单使用

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

namespace 认识LINQ
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arry = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            int[] arry2 = new int[] { 100 };
            var query1 = from n in arry select n;
            foreach (var item in query1)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("--------------------------------------");
            var query2 = from n in arry where n >= 5 select n;
            foreach (var item in query2)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("--------------------------------------");
            var query3 = from n in arry where n >= 5 orderby n descending select n;
            foreach (var item in query3)
            {
                Console.WriteLine(item);
            }

            var query4 = from a in arry from b in arry2 select (a + b) * 10;
            Console.WriteLine("--------------------------------------");
            foreach (var item in query4)
            {
                Console.WriteLine(item);
            }
            var query5 = from a in arry where a >= 3 select new { number = a * 10 };
            Console.WriteLine("--------------------------------------");
            foreach (var item in query5)
            {
                Console.WriteLine(item);
            }


            var query6 = from num in arry where num % 2 != 0 select num;
            Console.WriteLine("--------------------------------------");
            foreach (var item in query6)
            {
                Console.WriteLine(item);
            }
            var query7 = from num in arry let n = num % 2 where n == 0 select num;
            Console.WriteLine("--------------------------------------");
            foreach (var item in query7)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("--------------------------------------");
            var query8 = (from num in arry where num >= 5 select num).Min();
            Console.WriteLine(query8);


            List<string> list = new List<string>();

            list.Add("aa");

            list.Add("bb");

            list.Add("cc");

            var str = string.Join(":", list);

            Console.WriteLine(str);

            Console.WriteLine("********************************");
            var testList = new List<int>() { 1, 2, 3, 4, 5, 6 };
            var testResult = testList.LsWhere(x => x > 3);

            foreach (var item in testResult)
            {
                Console.WriteLine(item);
            }

            var t = from n in testList select n;

            Console.WriteLine("-----------------------Join--------------------------");
            Person[] persons = new Person[]
            {
                 new Person{ CityId = 1, Name = "廖xx" },
                 new Person{ CityId = 1, Name = "李漂亮"},
                 new Person{ CityId = 2, Name = "张美美"},
                 new Person{ CityId = 3, Name = "李思思"},
                 new Person{ CityId = 3, Name = "东施"},
                 new Person{ CityId = 4, Name = "西施"},

            };
            City[] cities = new City[]
            {
                new City{ Id = 1,Name = "ChongQing"},
                new City{ Id = 2,Name = "Shenzhen"},
                new City{ Id = 3,Name = "Beijing"},
                new City{ Id = 4,Name = "Shanghai"}
            };
            var result = persons.Join(cities, c => c.CityId, p => p.Id, (c, p) => new { PersonName = p.Name, CityName = c.Name }).ToList();
            foreach (var item in result)
            {
                Console.WriteLine($"{item.CityName}--{item.PersonName}");
                //Console.WriteLine(item.Key);
            }
            var group = from n in testList group n by n % 2 into res from n2 in res select n2;

            foreach (var i in group)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("----------------------------------------");
            var arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
            var arr2 = new int[] { 1, 2, 3, 4, 5 };

            var re = from n1 in arr1 join n2 in arr2 on n1 equals n2 into o from n3 in o orderby n3 descending select n3;
            var r = arr1.Join(arr2, x => x, y => y, (x, y) => x).OrderByDescending(x => x).ToList();
            foreach (var item in r)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("----------------------------------------");
            var testResult2 = testList.LsWhere2(x => x >= 5);
            foreach (var item in testResult2)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("----------------------------------------");
            Console.WriteLine(testList.TrueForAll(x => x >= 1));
            Console.WriteLine("----------------------------------------");
            Console.WriteLine(testList.Sum(x => x));
            Console.WriteLine("----------------------------------------");
            foreach (var item in testList.GetRange(0, 3))
            {
                Console.WriteLine(item);
            }
        }

    }
    class Person
    {
        public int CityId { set; get; }
        public string Name { set; get; }
    }
    class City
    {
        public int Id { set; get; }
        public string Name { set; get; }
    }

}

 

上一篇:Python面向对象编程


下一篇:Python中关于函数的操作