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

C#学习相关系列之Linq用法---where和select用法(二)

一、select用法

        Linq中的select可以便捷使我们的对List中的每一项进行操作,生成新的列表。

var tt=list.select(p=>p+10);
//select括号内为List中的每一项,p+10即为对每一项的操作,即对每项都加10生成新的List

用法实例:

1、lambda表达式

int[] array = { 1,5,6,7,6,9,12,2,7,6,33};
List<int> l1 = new List<int>(array);
var t1 = l1.Select((p)=>p+10);
foreach (var item in t1)
{Console.WriteLine(item);
}

输出结果为:

2、Linq语法

            List<Student_1> stuList = new List<Student_1>(){new Student_1(){ID=1,Name="John",Chinese=92,Math=88,English=92},new Student_1(){ID=2,Name="Mary",Chinese=87,Math=94,English=82},new Student_1(){ID=3,Name="KangKang",Chinese=89,Math=91,English=96},new Student_1(){ID=4,Name="Maria",Chinese=88,Math=65,English=94},new Student_1(){ID=5,Name="Ben",Chinese=70,Math=91,English=82},};var t1 = from e in stuList select e.English;foreach (var item in t1){Console.WriteLine(item);}

二、SelectMany用法

        在C# Linq中,SelectMany方法用于将一个集合中的每个元素转换为另一个集合,并将所有转换后的集合合并为一个集合。

List<List<int>> list = new List<List<int>>()
{new List<int>() { 1, 2, 3 },new List<int>() { 4, 5, 6 },new List<int>() { 7, 8, 9 }
};var result = list.SelectMany(x => x);foreach (var item in result)
{Console.WriteLine(item);
}

三、where用法

where在Linq中主要进行对数据筛选,并且生成新的List。

            List<Student_1> stuList = new List<Student_1>(){new Student_1(){ID=1,Name="John",Chinese=92,Math=88,English=92},new Student_1(){ID=2,Name="Mary",Chinese=87,Math=94,English=82},new Student_1(){ID=3,Name="KangKang",Chinese=89,Math=91,English=96},new Student_1(){ID=4,Name="Maria",Chinese=88,Math=65,English=94},new Student_1(){ID=5,Name="Ben",Chinese=70,Math=91,English=82},};//lambda表达式 表达式内部填的是判断条件var t1 = stuList.Where(p => p.English == 88);// Linq 语句var t1 = from e in stuList where e.English == 82 select e;

需要注意的是Lambda表达式中不需要select结尾,但Linq 语句必须是select结尾否则报错

运行结果为:

参考文献:

C#的LINQ select查询、where过滤、group分组、join关联_c# t.select().join_小龙在山东的博客-CSDN博客

http://www.lryc.cn/news/233268.html

相关文章:

  • 后端返回 date 时间日期格式为 UTC 格式字符串,形如 2022-08-11T10:50:31.050+00:00前端如何修改为yyyy-mm-dd
  • 【万字长文】前端性能优化实践 | 京东云技术团队
  • WPF位图效果
  • CFI(Common Flash Interface)简介
  • linux、windows 查看java等进程占用资源情况
  • 听GPT 讲Rust源代码--library/core/src(7)
  • html:lang属性设置为中文zh-CN
  • 滴滴 Redis 异地多活的演进历程
  • 前端实现页面内容的截图与下载(html2canvas)
  • VS2017 IDE 编译时的 X86、x64位 是干什么的
  • 微信小程序 解决tab页切换过快 数据出错问题
  • Taro编译警告解决方案:Error: chunk common [mini-css-extract-plugin]
  • 基于JavaWeb+SpringBoot+Vue电子商城微信小程序系统的设计和实现
  • JS进阶——作用域、解构、箭头函数
  • centos下安装mysql8版本
  • C++面试常考手写题目
  • LLM建模了什么,为什么需要RAG
  • 为开发GPT-5,OpenAI向微软寻求新融资
  • 创邻科技亮相ISWC 2023,国际舞台见证知识图谱领域研究突破
  • 开源博客项目Blog .NET Core源码学习(6:雪花算法)
  • 【Python】集合与字典
  • 【LeetCode】88. 合并两个有序数组
  • Linux文件权限
  • 〖大前端 - 基础入门三大核心之JS篇㉟〗- JavaScript 的DOM简介
  • CentOS中安装常用环境
  • python时间变化与字符串替换技术及读JSON文件等实践笔记
  • leetcode刷题日记:141. Linked List Cycle(环形链表)
  • html书本翻页效果,浪漫表白日记本(附源码)
  • 【Mysql】学习笔记
  • 工作记录-------java文件的JVM之旅(学习篇)---好理解