当前位置:网站首页>Exemple d'application de linq

Exemple d'application de linq

2022-01-15 02:11:30 La lave de Rodinia

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

namespace LinqDemo
{
    
    class Program
    {
    
        #region Exemple1:Non utiliséLINQTableau de requête

        static void Main(string[] args)
        {
    
            int[] nums = {
     1, 7, 2, 6, 5, 4, 9, 13, 20 };
            List<int> list = new List<int>();
            foreach (int item in nums)
            {
    
                if (item % 2 != 0)
                    list.Add(item);
            }
            list.Sort();
            list.Reverse();
            foreach (int item in list)
            {
    
                Console.WriteLine(item);
            }

            Console.ReadLine();
        }

        #endregion

        #region Exemple2:UtiliserLINQTableau des requêtes techniques

        //static void Main(string[] args)
        //{
    
        // int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

        // var list = from num in nums
        // where num % 2 != 0
        // orderby num descending
        // select num;

        // foreach (int item in list)
        // {
    
        // Console.WriteLine(item);
        // }
        // Console.ReadLine();
        //}

        #endregion

        #region Exemple3:Approche élargieSelect()Application

        //static void Main(string[] args)
        //{
    
        // int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

        // var list = nums.Select(item => item * item);
        // foreach (int item in list)
        // {
    
        // Console.WriteLine(item);
        // }

        // Console.ReadLine();
        //}

        #endregion

        #region Exemple4:Approche élargieWhere()Application

        //static void Main(string[] args)
        //{
    
        // int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

        // var list = nums
        // .Where(item => item % 2 == 0)
        // .Select(i => i * i);

        // Console.ReadLine();
        //}

        #endregion

        #region Exemple5:Approche élargieOrderBy()Application

        //static void Main(string[] args)
        //{
    
        // int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };
        // var list = nums
        // .Where(item => item % 2 == 0)
        // .Select(i => i * i)
        // .OrderBy(item => item);
        // foreach (int i in list)
        // {
    
        // Console.WriteLine(i);
        // }

        // Console.ReadLine();
        //}

        //static void Main(string[] args)
        //{
    
        // string[] nums = { "Zhang Yong", "Wang Qi", "Liu Jing", "Zhao Xinxin",
        // "Dolly.", " Ma juncai ", "Na - Young.", "Jackie Chan", };

        // var list = nums
        // .Where(item => item.Length == 2)
        // .Select(item => item)
        // .OrderByDescending(item => item.Substring(0, 1));
        // foreach (string item in list)
        // {
    
        // Console.WriteLine(item);
        // }

        // Console.ReadLine();
        //}

        #endregion

        #region Exemple6:Approche élargieGroupBy()Application
        //static void Main(string[] args)
        //{
    
        // string[] nums = { "Zhang Yong", "Wang Qi", "Liu Jing", "Zhao Xinxin",
        // "Dolly.", " Ma juncai ", "Na - Young.", "Jackie Chan","Wang Li", "Du Yu","Ma Xiao.","Liu Li","Madaha",};

        // var list = nums
        // .Where(item => item.Length == 2)
        // .Select(item => item)
        // .GroupBy(item => item.Substring(0, 1));

        // foreach (var groupItem in list)
        // {
    
        // Console.WriteLine("-------------------");
        // Console.WriteLine("Champs groupés:{0}", groupItem.Key);

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



        // Console.ReadLine();
        //}

        #endregion

        #region Exemple7:Mise en service du point d'arrêtLINQ Le moment de la requête 
        //static void Main(string[] args)
        //{
    
        // int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

        // var list = nums
        // .Where(item => item % 2 == 0)
        // .Select(item => item * item)
        // .OrderBy(item => item);

        // foreach (int i in list)
        // {
    
        // Console.WriteLine(i);
        // }
        // Console.ReadLine();
        //}

        #endregion

        #region Exemple8: Exécution immédiate de la requête 

        //static void Main(string[] args)
        //{
    
        // int[] nums = { 1, 7, 2, 6, 5, 4, 9, 13, 20 };

        // var list = nums
        // .Where(item => item % 2 == 0)
        // .Select(item => item * item)
        // .OrderBy(item => item)
        // .Count();

        // Console.WriteLine(list.ToString ());
        // Console.ReadLine();
        //}

        #endregion

        #region Exemple9:from Utilisation simple de la clause 

        //static void Main(string[] args)
        //{
    
        // ArrayList values = new ArrayList();
        // for (int i = 0; i < 10; i++) { values.Add(i); }
        // var list = from int item in values
        // where item % 2 == 0
        // select item;
        // foreach (int item in list) { Console.WriteLine(item); }
        // Console.ReadLine();
        //}

        #endregion

        #region Exemple10:ComposéfromUtilisation de la clause

        //static void Main(string[] args)
        //{
    
        // Student obj1 = new Student()
        // {
    
        // StuId = 1001,
        // StuName = "Stagiaires1",
        // ScoreList = new List<int>() { 90, 78, 54 }
        // };
        // Student obj2 = new Student()
        // {
    
        // StuId = 1002,
        // StuName = "Stagiaires2",
        // ScoreList = new List<int>() { 95, 88, 90 }
        // };
        // Student obj3 = new Student()
        // {
    
        // StuId = 1003,
        // StuName = "Stagiaires3",
        // ScoreList = new List<int>() { 79, 76, 89 }
        // };
        // // Encapsuler les délégués dans la collection 
        // List<Student> stuList = new List<Student>() { obj1, obj2, obj3 };
        // // Les résultats de la requête contiennent 95 Les participants ayant obtenu plus de points 
        // var result = from stu in stuList
        // from score in stu.ScoreList
        // where score >= 95
        // select stu;
        // //Afficher les résultats de la requête
        // foreach (var item in result)
        // {
    
        // Console.WriteLine(item.StuName);
        // }


        // Console.ReadLine();
        //}

        #endregion

        #region Exemple11:Plusieursfrom Utilisation de la requête clause 

        //static void Main(string[] args)
        //{
    
        // Student obj1 = new Student() { StuId = 1001, StuName = "Stagiaires1" };
        // Student obj2 = new Student() { StuId = 1009, StuName = "Stagiaires9" };
        // Student obj3 = new Student() { StuId = 1012, StuName = "Stagiaires12" };
        // Student obj4 = new Student() { StuId = 1003, StuName = "Stagiaires3" };
        // Student obj5 = new Student() { StuId = 1019, StuName = "Stagiaires19" };
        // Student obj6 = new Student() { StuId = 1006, StuName = "Stagiaires6" };

        // List<Student> stuList1 = new List<Student>() { obj1, obj2, obj3 };
        // List<Student> stuList2 = new List<Student>() { obj4, obj5, obj6 };

        // // L'apprentissage de la requête est supérieur à 1010Les cadets de
        // var result = from stu1 in stuList1
        // where stu1.StuId >= 1010
        // from stu2 in stuList2
        // where stu2.StuId >= 1010
        // select new { stu1, stu2 };
        // //Afficher les résultats de la requête
        // foreach (var item in result )
        // {
    
        // Console.WriteLine(item.stu1.StuName+" "+item.stu1.StuId);
        // Console.WriteLine(item.stu2.StuName + " " + item.stu2.StuId);
        // }

        // Console.ReadLine();
        //}

        #endregion

        #region Exemple12:Fonction d'agrégationCount

        //static void Main(string[] args)
        //{
    
        // Student obj1 = new Student() { StuId = 1001, StuName = "Stagiaires1" };
        // Student obj2 = new Student() { StuId = 1009, StuName = "Stagiaires9" };
        // Student obj3 = new Student() { StuId = 1012, StuName = "Stagiaires12" };
        // Student obj4 = new Student() { StuId = 1003, StuName = "Stagiaires3" };
        // Student obj5 = new Student() { StuId = 1019, StuName = "Stagiaires19" };
        // Student obj6 = new Student() { StuId = 1006, StuName = "Stagiaires6" };
        // List<Student> stuList = new List<Student>() { obj1, obj2, obj3, obj4, obj5, obj6 };

        // var count1 = (from c in stuList
        // where c.StuId > 1010
        // select c).Count();

        // var count2 = stuList.Where(c => c.StuId > 1010).Count();
        // Console.WriteLine("count1={0} count2={1}",count1,count2);


        // Console.ReadLine();
        //}
        #endregion

        #region Exemple13:Fonction d'agrégationMax、Min、Average

        //static void Main(string[] args)
        //{
    
        // Student obj1 = new Student() { StuId = 1001, Age = 22, StuName = "Stagiaires1" };
        // Student obj2 = new Student() { StuId = 1009, Age = 21, StuName = "Stagiaires9" };
        // Student obj3 = new Student() { StuId = 1012, Age = 25, StuName = "Stagiaires12" };
        // Student obj4 = new Student() { StuId = 1003, Age = 23, StuName = "Stagiaires3" };
        // Student obj5 = new Student() { StuId = 1019, Age = 27, StuName = "Stagiaires19" };
        // Student obj6 = new Student() { StuId = 1006, Age = 24, StuName = "Stagiaires6" };
        // List<Student> stuList = new List<Student>() { obj1, obj2, obj3, obj4, obj5, obj6 };

        // var maxAge = (from s in stuList
        // select s.Age).Max();
        // var minAge = stuList
        // .Select(s => s.Age).Min();
        // var avgAge = (from s in stuList
        // select s.Age).Average();
        // var sumAge = (from s in stuList
        // select s.Age).Sum();

        // Console.WriteLine("maxAge={0} minAge={1} avgAge={2} sumAge={3}", 
        // maxAge, minAge, avgAge,sumAge);

        // Console.ReadLine();
        //}

        #endregion

        #region Exemple14:Trier les classesThenByUtilisation de

        //static void Main(string[] args)
        //{
    

        // Student obj1 = new Student() { StuId = 1001, Age = 22, StuName = "Stagiaires1" };
        // Student obj2 = new Student() { StuId = 1009, Age = 21, StuName = "Stagiaires9" };
        // Student obj3 = new Student() { StuId = 1012, Age = 25, StuName = "Stagiaires12" };
        // Student obj4 = new Student() { StuId = 1003, Age = 23, StuName = "Stagiaires3" };
        // Student obj5 = new Student() { StuId = 1019, Age = 27, StuName = "Stagiaires19" };
        // Student obj6 = new Student() { StuId = 1006, Age = 24, StuName = "Stagiaires6" };
        // List<Student> stuList = new List<Student>() { obj1, obj2, obj3, obj4, obj5, obj6 };

        // var stus1 = from s in stuList
        // orderby s.StuName, s.Age, s.StuId
        // select s;

        // var stus2 = stuList
        // .OrderBy(s => s.StuName)
        // .ThenBy(s => s.Age)
        // .ThenBy(s => s.StuId)
        // .Select(p => p);

        // foreach (var s in stus1)
        // {
    
        // Console.WriteLine(s.StuName);
        // }

        // Console.WriteLine("----------------------");

        // foreach (var s in stus2)
        // {
    
        // Console.WriteLine(s.StuName);
        // }

        // Console.ReadLine();
        //}

        #endregion

        #region Exemple15: Requête de classe de partition 
        //static void Main(string[] args)
        //{
    
        // int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

        // var list1 = nums.Skip(1).Take(3);
        // var list2 = nums.SkipWhile(i => i % 3 != 0)
        // .TakeWhile(i => i % 2 != 0);

        // foreach (var item in list1) { Console.WriteLine(item); }
        // Console.WriteLine("------------");
        // foreach (var item in list2) { Console.WriteLine(item); }

        // Console.ReadLine();
        //}

        #endregion

        #region Exemple16: Requête de classe collection Distinct

        //static void Main(string[] args)
        //{
    
        // int[] nums = { 1, 2, 2, 6, 5, 6, 7, 8, 8 };
        // var list = nums.Distinct();

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

        // Console.ReadLine();
        //}

        #endregion

        #region Exemple17: Générer une requête de classe 

        //static void Main(string[] args)
        //{
    
        //var nums1 = Enumerable.Range(1, 10);
        // var nums2 = Enumerable.Repeat("LINQ best!", 10);

        // foreach (var item in nums1) { Console.WriteLine(item); }
        // Console.WriteLine("------------");
        // foreach (var item in nums2) { Console.WriteLine(item); }


        // Console.ReadLine();
        //}

        #endregion
    }
}

版权声明
本文为[La lave de Rodinia]所创,转载请带上原文链接,感谢
https://chowdera.com/2022/01/202201080558307133.html

随机推荐