Linq-1查询语言
查询语言我们了解很多,大体上他们的思路语言都是相同的,linq也不难,只是我们把它想的太复杂了而已。Linq语言集成化查询
基础:1,泛型 2,lambda
from 元素 in 集合
where 元素条件
orderby 元素.属性 ascending
group 元素 by 元素.属性
select 元素
看段练习吧...
//查询出集合中年龄大于25的学生
//查询出集合中年龄大于25,且以“哈”字开头的学生
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | using System; using System.Linq; using System.Collections.Generic; public class studyLinq { public static void Main() { /*获取他们的扩展名*/ string file = "sdfhue.gawei.ge..gaew.html" ; Console.WriteLine(file.GetFileType()); string ttt= "dfhue,ge.dfe.mp3" ; Console.WriteLine(ttt.GetFileType()); List<Student> list = new List<Student>(); list.Add( new Student(){Age=10,Name= "Jack" }); list.Add( new Student(){Age=67,Name= "Mack" }); list.Add( new Student(){Age=23,Name= "Dack" }); list.Add( new Student(){Age=26,Name= "哈密瓜" }); list.Add( new Student(){Age=8,Name= "Eack" }); list.Add( new Student(){Age=34,Name= "Hack" }); list.Add( new Student(){Age=18,Name= "小红" }); //查询出集合中年龄大于25的学生 IEnumerable<Student> result = list.Where<Student>(p=>p.Age>25); foreach (Student s in result) { Console.WriteLine(s.Age + " " + s.Name); } //查询出集合中年龄大于25,且以“哈”字开头的学生 IEnumerable<Student> result1 = list.Where(p => p.Age>25 &&p.Name.StartsWith( "哈" )); foreach (Student dd in result1) { Console.WriteLine(dd.Name + " " + dd.Age); } } public static void Test() { int [] a = {2,3,5,6,7,29,34,45,85,8,9,54,54,56,5}; //var c = a.Where(p=>{return p>20}); IEnumerable< int > c = a.Where(p=> p>20); foreach ( int d in c) { Console.WriteLine(d); } } } public class Student { public string Name { get ; set ; } public int Age { get ; set ; } } public static class ExetendMethod { public static string GetFileType( this string str) { string [] strs = str.Split( '.' ); return strs[strs.Length-1]; } } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法