开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 1433|回复: 3
收起左侧

[C#图文教程] 易转C# - 9 - Linq

[复制链接]
结帖率:95% (84/88)
发表于 2023-8-28 06:52:22 | 显示全部楼层 |阅读模式   泛播地址
本帖最后由 陽陽陽 于 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              

结帖率:95% (84/88)

签到天数: 23 天

 楼主| 发表于 2023-8-28 10:41:33 | 显示全部楼层   泛播地址
铅笔刀 发表于 2023-8-28 09:27
[mw_shl_code=csharp,true][DllImport("user32.dll")]
private static extern bool SendNotifyMessage(uint ...

不知道,不清楚/捂脸
回复 支持 反对

使用道具 举报

结帖率:97% (36/37)

签到天数: 4 天

发表于 2023-8-28 09:39:24 | 显示全部楼层   江西省南昌市
好巧,我也在学C#
回复 支持 反对

使用道具 举报

结帖率:80% (4/5)

签到天数: 24 天

发表于 2023-8-28 09:27:07 | 显示全部楼层   广东省惠州市
[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#转易可以吗
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表