当前位置:
首页 > Python基础教程 >
-
C#教程之【Linq】常用语法汇总
语言继承查询(Language Integrated Query, LINQ),在C#编程语言中集成了查询语法,可以用相同的语法访问不同的数据源,LINQ提供了不同数据源的抽象层,所以可以使用相同语法。
public class Book { public int Id { get; set; } /// <summary> /// 书名 /// </summary> public string BookName { get; set; } /// <summary> /// 作者id /// </summary> public int AutherId { get; set; } /// <summary> /// 类型 /// </summary> public string Type { get; set; } /// <summary> /// 售价 /// </summary> public decimal Price { get; set; } /// <summary> /// 销量 /// </summary> public int Sales { get; set; } } public class Auther { public int Id { get; set; } /// <summary> /// 作者 /// </summary> public string AutherName { get; set; } } public class Library { public string Address { get; set; } /// <summary> /// 书本 /// </summary> public List<Book> BookList { get; set; } }
1、条件查询(Where)
var query = from book in bookList where book.Price > 50 orderby book.Sales descending,book.BookName select book; //等同于 var query = bookList.Where(n => n.Price > 50).OrderByDescending(g => g.Sales).ThenBy(y => y.BookName);
需要注意的是,延迟查询
var ary = new List<string>(){ "Aa", "Bb", "Cc"}; var a1 = ary.Where(n => n.Contains("a"));//["Aa"] ary.Add("Ga"); a1;//["Aa", "Ga"]
解决这个问题,只需要用 ToList();
var ary = new List<string>(){ "Aa", "Bb", "Cc"}; var a1 = ary.Where(n => n.Contains("a")).ToList();//["Aa"] ary.Add("Ga"); a1;//["Aa"]
索引筛选
//销量大于50且为奇数行 var query = bookList.Where((n, index) => n.Sales > 50 && index%2 != 0);
2、复合查询(SelectMany)
//查找所有图书馆中的小说 var query = from library in libraryList from book in library.BookList where book.Type == "小说" select book; //等同于 query = libraryList.SelectMany(n => n.BookList).Where(g => g.Type == "小说"); //组织返回结果 query = libraryList.SelectMany(n => n.BookList, (n, g) => new {n.Address, g.BookName, g.Sales}).Where(y => y.Sales > 100);
3、集合连接
//内连接 var query1 = from book in bookList join auther in autherList on book.AutherId equals auther.Id select new { book.BookName, auther.AutherName }; //组连接 var query2 = from auther in autherList join book in bookList on auther.Id equals book.AutherId into items select new { auther.AutherName, Books = items }; //左外连接 var query3 = from book in bookList join auther in autherList on book.AutherId equals auther.Id into items from auther in items.DefaultIfEmpty() select new { book.BookName, AutherName = auther == default(Auther) ? "无" : auther.AutherName }; //多条件连接 var query4 = from book in bookList join auther in autherList on new {name = book.BookName, id = book.AutherId} equals new {name = auther.AutherName, id = auther.Id} select book;
4、排序(OrderBy、ThenBy)
var query = from book in bookList orderby book.Sales descending, book.AutherId, book.Price descending select book; //等同于 query = bookList.OrderByDescending(n => n.Sales).ThenBy(g => g.AutherId).ThenByDescending(y => y.Price);
5、分组(GroupBy)
//单一条件分组 var query = from book in bookList group book by book.Type into bs select bs.First(); //等同于 query = bookList.GroupBy(n => n.Type).Select(g => g.First()); //多条件分组 var query = from book in bookList group book by new { book.Type, book.AutherId } into bs select new { Type = bs.First().Type, AutherId = bs.First().AutherId, Count = bs.Count() }; //等同于 query = bookList.GroupBy(n => new {n.Type, n.AutherId}).Select(g => new { Type = g.First().Type, AutherId = g.First().AutherId, Count = g.Count() });
6、合并与分区(Zip、Take、Skip)
int[] numbers = { 1, 2, 3 }; string[] words = { "One", "Two", "Three", "Four" }; //元素依次组合,长度为较小的集合 IEnumerable<string> zip = numbers.Zip(words, (n, g) => n + "=" + g);//["1=One", "2=Two", "3=Three"] //跳过集合的前n个元素 var skip = words.Skip(3);//["Four"] //获取集合的前n个元素,有延迟 var take = numbers.Take(2);//[1, 2] int pageSize = 3;//每页容量 int pageNum = 0;//页数 var page = words.Skip(pageSize * pageNum).Take(pageSize);//["One", "Two", "Three"]
7、集合操作(Distinct、Union、Concat、Intersect、Except)
int[] ary1 = {1, 2, 2, 4, 5}; int[] ary2 = {3, 5, 5, 6, 10, 7}; //合并,自动去重 var union = ary1.Union(ary2);//1, 2, 3, 4, 5, 6, 7, 10 //合并,不会去重 var concat = ary1.Concat(ary2);//1, 2, 2, 4, 5, 3, 5, 5, 6, 10, 7 //去重 var distict = ary1.Distinct();//1, 2, 4, 5 //取交集,自动去重 var intersect = ary1.Intersect(ary2);//5 //取补集,自动去重 var except = ary1.Except(ary2);//1, 2, 4
8、类型筛选(ofType)
object[] data = { "one", 1 , 2 ,"three"}; var query = data.ofType<string>();
9、聚合操作符(Count、Sum、Min、Max、Average、Aggregate)
//个数 var count = bookList.Count(n => n.Sales > 50); //求和 var sum = bookList.Sum(n => n.Price); //最小值 var min = bookList.Min(n => n.Sales); //最大值 var max = bookList.Max(n => n.Price); //平均值 var average = bookList.Average(n => n.Sales); //累加,总销量 var aggregate1 = bookList.Select(n => n.Sales).Aggregate((g, y) => g + y); //累加,初始值 var aggregate2 = bookList.Select(n => n.Sales).Aggregate(10, (g, y) => g + y); //累加,初始值,结果处理 var aggregate3 = bookList.Select(n => n.Sales).Aggregate(10, (g, y) => g + y, result => result/100);
10、转换操作符 (ToArray、ToDictionary、ToList、ToLookup、Cast)
Book[] ary = bookList.ToArray(); List<Book> list = bookList.ToList(); Dictionary<int, Book> dic = bookList.ToDictionary<Book, int>(n => n.Id); //转换成LookUp集合,key-以key分组的内部集合 ILookup<string, Book> look = bookList.ToLookup(n => n.Type); IEnumerable<Book> cast = ary.Cast<Book>();
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式