精易论坛

标题: 易转C# - 9 - Linq [打印本页]

作者: 陽陽陽    时间: 2023-8-28 06:52
标题: 易转C# - 9 - Linq
本帖最后由 陽陽陽 于 2023-8-28 06:53 编辑

上代码!


[C#] 纯文本查看 复制代码
using System;

using System.Linq;

using System.Collections.Generic;



namespace LinqExample

{

    class Program

    {

        private static Random rd = new Random();

        static void Main(string[] args)

        {

            OrderByExample();

        }

        public static void WhereExample()

        {

            string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba", "Fatimah" };

            foreach (var b in names.Where(a => a.StartsWith('S')))

            {

                Console.WriteLine(b);

            }

        }

        public static void SelectExample()

        {

            List<SelectStruct> ls = new List<SelectStruct>();

            for (int i = 0; i < 2000000; i++)

            {

                ls.Add(new SelectStruct(GetRdName(),i));

            }



            foreach (var item in

                ls.

                Where(c => c.name.StartsWith('g')).

                Where(d => d.name.EndsWith('A')).

                Where(f => f.name.Contains('f')).

                Where(f => f.name.Contains('[')).

                Select(a => a.name + " " + a.id.ToString())





                )

            {

                Console.WriteLine(item);

            }

            

            //10进制 - 65~122

        }

        public static void AllAnyExample()

        {

            List<SelectStruct> ls = new List<SelectStruct>();

            for (int i = 0; i < 20000; i++)

            {

                ls.Add(new SelectStruct(GetRdName().Substring(1,2), i));

            }

            Console.WriteLine(ls.All(d => d.name == "xY"));

            Console.WriteLine(ls.Any(d => d.name == "xY"));



        }





        [Obsolete("此版本不支持此方法",true)]

        public static void ChunkExample()

        {

            //Chunk

            //string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba", "Fatimah" };

            //foreach(var i in names.Chunk)







            /*

            

             适用于

             产品        版本

             .NET        6, 7, 8





             */

        }

        public static void AverageExample()

        {

            List<int> ls = new List<int>(){ 1, 2, 3, 4, 5, 5, 3, 56, 3, 3, 35, 32 };

            Console.WriteLine(ls.Average());

        }

        public static void LastOrDefaultExample()

        {

            string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba", "Fatimah" };

            Console.WriteLine(names.LastOrDefault(x => x.StartsWith("S")));

            Console.WriteLine(names.FirstOrDefault(x => x.StartsWith("S"))); ;

        }

        public static void UpperLowerCaseTest()

        {

            string[] list = {"smith","Student","stupid"}; //区分大小写

            Console.WriteLine(list.FirstOrDefault(x => x.StartsWith("S")));

        }

        public static void OrderByDescendingExample()

        {

            string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba", "Fatimah" };

            foreach(var i in names.OrderByDescending(x => x, new CompareByLen()))

            {

                Console.WriteLine(i);

            }

        }

        public static void OrderByExample()

        {

            string[] names = { "Alonso", "Zheng", "Smith", "Jones", "Smythe", "Small", "Ruiz", "Hsieh", "Jorgenson", "Ilyich", "Singh", "Samba", "Fatimah" };

            foreach (var i in names.OrderBy(x => 9999 - x.Length)) //使用x.lenth,会自动分配到每个变量,然后进行升序排列 ---- 用99999 - lenth,就是降序排列

            {

                Console.WriteLine(i);

            }

        }





        //************************************************华(hua)丽(ji)的分割线************************************************





        private static string GetRdName()

        {

            string name = "";

            for (int i = 0; i < 5; i++)

            {

                name += (char)rd.Next(65, 122);

            }

            return name;

        }

        private struct SelectStruct

        {

            public SelectStruct(string name, int id)

            {

                this.name = name;

                this.id = id;

            }

            public string name;

            public int id;

        }

    }



    public class CompareByLen : IComparer<string>

    {

        public int Compare(string x, string y)

        {

            return x.Length - y.Length;

            //CompareTo 方法返回一个整数,表示两个值的相对顺序。

            //如果第一个值小于第二个值,则返回一个负数;

            //如果第一个值等于第二个值,则返回零;

            //如果第一个值大于第二个值,则返回一个正数。

        }

    }

}




个人学习线路:

Muiltithreading             √
File                        √
Linq                        √
JSON / XML              乄  
Database                    
Bitmap                     
HttpClient                  
RegExp                       
tcp                          
Win32 API                  
Script / HOOK               
asp.net                     
Web VPS Project              


作者: 铅笔刀    时间: 2023-8-28 09:27
[C#] 纯文本查看 复制代码
[DllImport("user32.dll")]
private static extern bool SendNotifyMessage(uint hWnd, uint Msg, IntPtr wPARAM, IntPtr lPARAM);

const uint HWND_BROADCAST = 0xffff;
const uint WM_SETTINGCHANGE = 0x001A;
SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, new IntPtr(0), Marshal.StringToHGlobalAnsi("ImmersiveColorSet"));



那么C#转易可以吗

作者: StarAdmire    时间: 2023-8-28 09:39
好巧,我也在学C#
作者: 陽陽陽    时间: 2023-8-28 10:41
铅笔刀 发表于 2023-8-28 09:27
[mw_shl_code=csharp,true][DllImport("user32.dll")]
private static extern bool SendNotifyMessage(uint ...

不知道,不清楚/捂脸




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4