当前位置: 首页 > news >正文

C# LING查询语法学习,扩展方法的使用

class Program
{
    #region  示例1:不使用LINQ查询数组

    //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 示例2:使用LINQ技术查询数组

    //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 示例3:扩展方法Select()应用

    //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  示例4:扩展方法Where()应用

    //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);
    //    foreach (int item in list)
    //    {
    //        Console.WriteLine(item);
    //    }
    //    Console.ReadLine();
    //}

    #endregion

    #region 示例5:扩展方法OrderBy()应用

    //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 = { "张勇", "王琦", "刘静", "赵鑫鑫",
    //                        "杜丽", "马俊才", "那英", "成龙", };

    //    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 示例6:扩展方法GroupBy()应用
    //static void Main(string[] args)
    //{
    //    string[] nums = { "张勇", "王琦", "刘静", "赵鑫鑫",
    //                        "杜丽", "马俊才", "那英", "成龙","王丽", "杜宇","马晓","刘丽","马大哈",};

    //    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("分组字段:{0}", groupItem.Key);

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

    //    Console.ReadLine();
    //}

    #endregion

    #region  示例7:断点调试LINQ的查询时机
    //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 示例8:查询的立即执行

    //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  示例9:from子句的简单使用

    //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 示例10:复合from子句的使用

    //static void Main(string[] args)
    //{
    //    Student obj1 = new Student()
    //    {
    //        StuId = 1001,
    //        StuName = "学员1",
    //        ScoreList = new List<int>() { 90, 78, 54 }
    //    };
    //    Student obj2 = new Student()
    //    {
    //        StuId = 1002,
    //        StuName = "学员2",
    //        ScoreList = new List<int>() { 95, 88, 90 }
    //    };
    //    Student obj3 = new Student()
    //    {
    //        StuId = 1003,
    //        StuName = "学员3",
    //        ScoreList = new List<int>() { 79, 76, 89 }
    //    };
    //    将学员封装到集合中
    //    List<Student> stuList = new List<Student>() { obj1, obj2, obj3 };
    //    查询成绩包含95分以上的学员
    //    var result = from stu in stuList
    //                 from score in stu.ScoreList
    //                 where score >= 90
    //                 select stu;
    //    显示查询结果
    //    foreach (var item in result)
    //    {
    //        Console.WriteLine(item.StuName);
    //    }


    //    Console.ReadLine();
    //}

    #endregion

    #region 示例11:多个from子句查询的使用

    //static void Main(string[] args)
    //{
    //    Student obj1 = new Student() { StuId = 1001, StuName = "学员1" };
    //    Student obj2 = new Student() { StuId = 1009, StuName = "学员9" };
    //    Student obj3 = new Student() { StuId = 1012, StuName = "学员12" };
    //    Student obj4 = new Student() { StuId = 1003, StuName = "学员3" };
    //    Student obj5 = new Student() { StuId = 1019, StuName = "学员19" };
    //    Student obj6 = new Student() { StuId = 1006, StuName = "学员6" };

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

    //    //查询学号大于1010的学员
    //    var result = from stu1 in stuList1
    //                 where stu1.StuId >= 1010
    //                 from stu2 in stuList2
    //                 where stu2.StuId >= 1010
    //                 select new { stu1, stu2 };
    //    //显示查询结果
    //    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 示例12:聚合函数Count

    //static void Main(string[] args)
    //{
    //    Student obj1 = new Student() { StuId = 1001, StuName = "学员1" };
    //    Student obj2 = new Student() { StuId = 1009, StuName = "学员9" };
    //    Student obj3 = new Student() { StuId = 1012, StuName = "学员12" };
    //    Student obj4 = new Student() { StuId = 1003, StuName = "学员3" };
    //    Student obj5 = new Student() { StuId = 1019, StuName = "学员19" };
    //    Student obj6 = new Student() { StuId = 1006, StuName = "学员6" };
    //    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 示例13:聚合函数Max、Min、Average

    //static void Main(string[] args)
    //{
    //    Student obj1 = new Student() { StuId = 1001, Age = 22, StuName = "学员1" };
    //    Student obj2 = new Student() { StuId = 1009, Age = 21, StuName = "学员9" };
    //    Student obj3 = new Student() { StuId = 1012, Age = 25, StuName = "学员12" };
    //    Student obj4 = new Student() { StuId = 1003, Age = 23, StuName = "学员3" };
    //    Student obj5 = new Student() { StuId = 1019, Age = 27, StuName = "学员19" };
    //    Student obj6 = new Student() { StuId = 1006, Age = 24, StuName = "学员6" };
    //    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 示例14:排序类ThenBy的使用

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

    //    Student obj1 = new Student() { StuId = 1001, Age = 22, StuName = "学员1" };
    //    Student obj2 = new Student() { StuId = 1009, Age = 21, StuName = "学员9" };
    //    Student obj3 = new Student() { StuId = 1012, Age = 25, StuName = "学员12" };
    //    Student obj4 = new Student() { StuId = 1003, Age = 23, StuName = "学员3" };
    //    Student obj5 = new Student() { StuId = 1019, Age = 27, StuName = "学员19" };
    //    Student obj6 = new Student() { StuId = 1006, Age = 24, StuName = "学员6" };
    //    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 示例15:分区类查询
    //static void Main(string[] args)
    //{
    //    int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    //    //Skip方法用于跳过序列中指定数量的元素,并返回剩余的元素
    //    //Take方法用于从序列中获取指定数量的元素
    //    //SkipWhile方法用于跳过序列中满足指定条件的元素,直到遇到第一个不满足条件的元素为止
    //    //用于从序列的开头开始返回满足指定条件的元素,直到遇到第一个不满足条件的元素为止
    //    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 示例16:集合类查询Distinct

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

    //    //使用LINQ的Distinct方法可以从集合中筛选出不重复元素
    //    var list = nums.Distinct();

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

    //    Console.ReadLine();
    //}

    #endregion

    #region  示例17:生成类查询

    static void Main(string[] args)
    {
        //Enumerable.Range方法用于生成一个指定范围内的整数序列

        // Enumerable.Repeat方法用于创建一个包含指定元素重复多次的序列。它接受两个参数:要重复的元素和重复次数
        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
}

 class Student
 {
     public int StuId { get; set; }
     public int Age { get; set; }
     public string StuName { get; set; }
     public List<int> ScoreList { get; set; }

 }

相关文章:

C# LING查询语法学习,扩展方法的使用

class Program { #region 示例1&#xff1a;不使用LINQ查询数组 //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) …...

自然语言推断:微调BERT

微调BERT 自然语言推断任务设计了一个基于注意力的结构。现在&#xff0c;我们通过微调BERT来重新审视这项任务。自然语言推断是一个序列级别的文本对分类问题&#xff0c;而微调BERT只需要一个额外的基于多层感知机的架构&#xff0c;如下图中所示。 本节将下载一个预训练好的…...

立创EDA学习:设计收尾工作

布线整理 ShiftM&#xff0c;关闭铺铜显示 调整结束后再使用快捷键”ShiftM“打开铺铜 过孔 在空白区域加上一些GND过孔&#xff0c;连接顶层与底层的铺铜。放置好”过孔“后&#xff0c;隐藏铺铜&#xff0c;观察刚才放置的过孔有没有妨碍到其他器件 调整铺铜 先打开铺铜区&…...

ShardingSphere之ShardingJDBC客户端分库分表上

目录 什么是ShardingSphere&#xff1f; 客户端分库分表与服务端分库分表 ShardingJDBC客户端分库分表 ShardingProxy服务端分库分表 ShardingSphere实现分库分表的核心概念 ShardingJDBC实战 什么是ShardingSphere&#xff1f; ShardingSphere是一款起源于当当网内部的应…...

rust for循环步长-1,反向逆序遍历

fn main() {for i in (0..3).rev().step_by(1) {print!("{}", i);} } // 打印结果&#xff1a;210Trait std::iter::Iterator fn rev(self) -> Rev< Self > where Self: Sized DoubleEndedIteratorfn step_by(self, step: usize) -> StepBy< Self &…...

编译与运行环境(C语言)

文章目录 前言编译环境编译链接 运行环境 前言 C语言代码的实现&#xff0c;存在两种不同的环境。 第一种是翻译环境&#xff0c;在这个环境中&#xff0c;源代码被转换为可执行的二进制指令。 翻译环境即我们日常使用编译器&#xff0c;将一个 " mission.c " 的文件…...

再谈Android View绘制流程

一&#xff0c;先思考何时开始绘制 笔者在这里提醒读者&#xff0c;Android的View是UI的高级抽象&#xff0c;我们平时使用的XML文件也好&#xff0c;本质是设计模式中的一种策略模式&#xff0c;其View可以理解为一种底层UI显示的Request。各种VIew的排布&#xff0c;来自于开…...

分布式定时任务系列8:XXL-job源码分析之远程调用

传送门 分布式定时任务系列1&#xff1a;XXL-job安装 分布式定时任务系列2&#xff1a;XXL-job使用 分布式定时任务系列3&#xff1a;任务执行引擎设计 分布式定时任务系列4&#xff1a;任务执行引擎设计续 分布式定时任务系列5&#xff1a;XXL-job中blockingQueue的应用 …...

python+Qt5 UOS 摄相头+麦克风测试,摄相头自动解析照片二维条码,麦克风解析音频文件

UI图片&#xff1a; 源代码&#xff1a; # -*- coding: utf-8 -*-# Form implementation generated from reading ui file CameraTestFrm.ui # # Created by: PyQt5 UI code generator 5.15.2 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is…...

MongoDB日期存储与查询、@Query、嵌套字段查询实战总结

缘由 MongoDB数据库如下&#xff1a; 如上截图&#xff0c;使用MongoDB客户端工具DataGrip&#xff0c;在filter过滤框输入{ profiles.alias: 逆天子, profiles.channel: }&#xff0c;即可实现昵称和渠道多个嵌套字段过滤查询。 现有业务需求&#xff1a;用Java代码来查询…...

Windows版本Node.js常见问题及操作解决方式(小白入门必备)

npm i时ERROR:reason: certificate has expired问题 https://blog.csdn.net/m0_73360677/article/details/135774500 # 1.取消ssl验证&#xff1b;npm config set strict-ssl false#这个方法一般可以直接解决问题&#xff0c;如不能请尝试第二种方法# 2.更换npm镜像源&#x…...

09.Elasticsearch应用(九)

Elasticsearch应用&#xff08;九&#xff09; 1.搜索结果处理包括什么 排序分页高亮返回指定字段 2.排序 介绍 Elasticsearch支持对搜索结果排序&#xff0c;默认是根据相关度算分来排序 支持排序的字段 keyword数值地理坐标日期类型 排序语法 GET /[索引名称]/_sear…...

ROS2常用命令工具

ROS2常用命令工具 包管理工具ros2 pkg ros2 pkg create ros2 pkg create --build-type ament_python pkg_name rclpy std_msgs sensor_msgs –build-type : C或者C ament_cmake &#xff0c;Python ament_python pkg_name &#xff1a;创建功能包的名字 rclpy std_msgs sens…...

Linux之快速入门

一、Linux目录结构 从Windows转到Linux最不习惯的是什么&#xff1a; 目录结构 Windows会分盘&#xff0c;想怎么放东西就怎么放东西&#xff0c;好处自由&#xff0c;缺点容易乱 Linux有自己的目录结构&#xff0c;不能随随便便放东西 /&#xff1a;根目录/bin:二进制文件&…...

C语言——操作符详解1

目录 1. 操作符的分类2. 二进制和进制转换2.1 二进制的概念2.2 二进制转十进制2.3 十进制转二进制2.4 二进制转八进制和十六进制2.4.1 二进制转八进制二进制转十六进制 3. 原码、反码和补码4. 移位操作符4.1 左移操作符4.2 右移操作符 5. 位操作符5.1 &5.2 |5.3 ^5.4 ~ 1. …...

C++学习| QT快速入门

QT简单入门 QT Creater创建QT项目选择项目类型——不同项目类型的区别输入项目名字和路径选择合适的构建系统——不同构建系统的却别选择合适的类——QT基本类之间的关系Translation File选择构建套件——MinGW和MSVC的区别 简单案例&#xff1a;加法器设计界面——构建加法器界…...

Android App开发-简单控件(1)——文本显示

本章介绍了App开发常见的几类简单控件的用法&#xff0c;主要包括&#xff1a;显示文字的文本视图、容纳视图的常用布局、响应点击的按钮控件、显示图片的图像视图等。然后结合本章所涉及的知识&#xff0c;完成一个实战项目“简单计算器”的设计与实现。 1.1 文本显示 本节介绍…...

[GYCTF2020]Ezsqli1

打开环境&#xff0c;下面有个提交表单 提交1&#xff0c;2有正确的查询结果&#xff0c;3以后都显示Error Occured When Fetch Result. 题目是sql&#xff0c;应该考察的是sql注入 简单fuzz一下 发现information_schema被过滤了&#xff0c;猜测是盲注了。 测试发现只要有东…...

【npm包】如何发布自己的npm包

随着Node.js的普及&#xff0c;npm&#xff08;Node Package Manager&#xff09;已成为JavaScript开发者中不可或缺的一部分。发布自己的npm包&#xff0c;不仅可以将自己的项目分享给更多人&#xff0c;还可以为社区做出贡献。本文将详细介绍如何从零开始发布自己的npm包。 …...

《WebKit技术内幕》学习之十五(2):Web前端的未来

2 嵌入式应用模式 2.1 嵌入式模式 读者可能会奇怪本章重点表达的是Web应用和Web运行平台&#xff0c;为什么会介绍嵌入式模式&#xff08;Embedded Mode&#xff09;呢&#xff1f;这是因为很多Web运行平台是基于嵌入式模式的接口开发出来的&#xff0c;所以这里先解释一下什…...

Chapter03-Authentication vulnerabilities

文章目录 1. 身份验证简介1.1 What is authentication1.2 difference between authentication and authorization1.3 身份验证机制失效的原因1.4 身份验证机制失效的影响 2. 基于登录功能的漏洞2.1 密码爆破2.2 用户名枚举2.3 有缺陷的暴力破解防护2.3.1 如果用户登录尝试失败次…...

Cursor实现用excel数据填充word模版的方法

cursor主页&#xff1a;https://www.cursor.com/ 任务目标&#xff1a;把excel格式的数据里的单元格&#xff0c;按照某一个固定模版填充到word中 文章目录 注意事项逐步生成程序1. 确定格式2. 调试程序 注意事项 直接给一个excel文件和最终呈现的word文件的示例&#xff0c;…...

51c自动驾驶~合集58

我自己的原文哦~ https://blog.51cto.com/whaosoft/13967107 #CCA-Attention 全局池化局部保留&#xff0c;CCA-Attention为LLM长文本建模带来突破性进展 琶洲实验室、华南理工大学联合推出关键上下文感知注意力机制&#xff08;CCA-Attention&#xff09;&#xff0c;…...

Admin.Net中的消息通信SignalR解释

定义集线器接口 IOnlineUserHub public interface IOnlineUserHub {/// 在线用户列表Task OnlineUserList(OnlineUserList context);/// 强制下线Task ForceOffline(object context);/// 发布站内消息Task PublicNotice(SysNotice context);/// 接收消息Task ReceiveMessage(…...

基于服务器使用 apt 安装、配置 Nginx

&#x1f9fe; 一、查看可安装的 Nginx 版本 首先&#xff0c;你可以运行以下命令查看可用版本&#xff1a; apt-cache madison nginx-core输出示例&#xff1a; nginx-core | 1.18.0-6ubuntu14.6 | http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages ng…...

Linux简单的操作

ls ls 查看当前目录 ll 查看详细内容 ls -a 查看所有的内容 ls --help 查看方法文档 pwd pwd 查看当前路径 cd cd 转路径 cd .. 转上一级路径 cd 名 转换路径 …...

【快手拥抱开源】通过快手团队开源的 KwaiCoder-AutoThink-preview 解锁大语言模型的潜力

引言&#xff1a; 在人工智能快速发展的浪潮中&#xff0c;快手Kwaipilot团队推出的 KwaiCoder-AutoThink-preview 具有里程碑意义——这是首个公开的AutoThink大语言模型&#xff08;LLM&#xff09;。该模型代表着该领域的重大突破&#xff0c;通过独特方式融合思考与非思考…...

Qwen3-Embedding-0.6B深度解析:多语言语义检索的轻量级利器

第一章 引言&#xff1a;语义表示的新时代挑战与Qwen3的破局之路 1.1 文本嵌入的核心价值与技术演进 在人工智能领域&#xff0c;文本嵌入技术如同连接自然语言与机器理解的“神经突触”——它将人类语言转化为计算机可计算的语义向量&#xff0c;支撑着搜索引擎、推荐系统、…...

Robots.txt 文件

什么是robots.txt&#xff1f; robots.txt 是一个位于网站根目录下的文本文件&#xff08;如&#xff1a;https://example.com/robots.txt&#xff09;&#xff0c;它用于指导网络爬虫&#xff08;如搜索引擎的蜘蛛程序&#xff09;如何抓取该网站的内容。这个文件遵循 Robots…...

学校时钟系统,标准考场时钟系统,AI亮相2025高考,赛思时钟系统为教育公平筑起“精准防线”

2025年#高考 将在近日拉开帷幕&#xff0c;#AI 监考一度冲上热搜。当AI深度融入高考&#xff0c;#时间同步 不再是辅助功能&#xff0c;而是决定AI监考系统成败的“生命线”。 AI亮相2025高考&#xff0c;40种异常行为0.5秒精准识别 2025年高考即将拉开帷幕&#xff0c;江西、…...