ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# LINQ简单使用

2022-01-21 17:34:32  阅读:157  来源: 互联网

标签:Console Name C# LINQ item WriteLine 简单 var new


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; }
    }

}

 

标签:Console,Name,C#,LINQ,item,WriteLine,简单,var,new
来源: https://www.cnblogs.com/hljjway/p/15831061.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有