C# 之 LINQ(二)

C# 之 LINQ(二)

  • 在C#中使用LINQ
  • 标准查询操作符
  • 使用扩展方法
    • 最简单的查询Select
    • 使用Where
    • 排序
    • 分组
    • 自定义变量
    • 内连接
    • 左外连接
    • 组连接
    • 集合操作
      • 去重Disinct()
      • 交集Intersect()
      • 并集Union()
      • 补集Except()
      • 合并Zip()
    • 分页
  • 小结

在C#中使用LINQ

在上一章节中,刘叔创建了一堆数据,这一章节就使用LINQ来查询这些数据。

标准查询操作符

标准查询操作符 说明
Where
OfType
筛选操作符 Where中可以使用谓词 例如lambda表达式定义的谓词 OfType根据类型筛选元素
Select
SelectMany
投射操作符
OrderBy
ThenBy
OrderByDescending
ThenByDescending
Reverse
排序操作符 OrderBy升序 OrderByDescending降序 ThenBy和ThenByDescending进行二次排序 Reverse反转元素
Join
GroupJoin
连接操作符 Join根据键选择器函数连接两个集合 类似于SQL中的JOIN GroupJoin连接两个集合组合其结果
GroupBy
ToLookup
组合操作符 GroupBy组合公有键的元素 ToLookup通过创建一个一对多的字典来组合元素
Any
All
Contains
限定符操作符 Any确定集合中是否有满足谓词函数的元素 All确定集合中所以元素是否都满足谓词函数 Contains检查某个元素是否在集合中
Take
Skip
TakeWhile
SkipWhile
分区操作符 返回集合的一个子集 Take必须指定要从集合中提取的元素个数 Skip跳过指定的元素个数 TakeWhile提取条件为真的元素 SkipWhile跳过条件为真的元素
Distinct
Union
Intersect
Except
Zip
Set操作符 返回一个集合 Distinct从集合中删除重复的元素 Union返回出现在其中一个集合中的唯一元素 Intersect返回交集 Except返回只出现在一个集合中的元素 Zip把两个集合合并为一个
First
FirstOrDefault
Last
LastOrDefault
ElementAt
ElementAtOrDefault
Single
SingleOrDefault
这些操作符只返回一个元素 First返回第一个满足条件的元素 FirstOrDefault无满足条件元素返回默认值 Last返回最后一个满足条件的元素 ElementAt指定了要返回元素的位置 Single只返回一个满足条件的元素 如果有多个就抛出异常
Count
Sum
Min
Max
Average
Aggregate
聚合操作符
ToArray
AsEnumerable
ToList
ToDictionary
Cast
将集合转换成数组
Empty
Range
Repeat
返回一个新集合 Empty返回空集合 Range返回一系列数字 Repeat返回一个始终重复一个值的集合

使用扩展方法

最简单的查询Select

using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var query = category.Select(row => row);
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

{“category_id”: 1, “category_name”: “HTML / CSS”}
{“category_id”: 2, “category_name”: “JavaScript”}
{“category_id”: 3, “category_name”: “服务端”}
{“category_id”: 4, “category_name”: “数据库”}
{“category_id”: 5, “category_name”: “移动端”}
{“category_id”: 6, “category_name”: “XML 教程”}
{“category_id”: 7, “category_name”: “ASP.NET”}
{“category_id”: 8, “category_name”: “Web Service”}
{“category_id”: 9, “category_name”: “开发工具”}
{“category_id”: 10, “category_name”: “网站建设”}

上述代码是查询全部内容

使用Where

using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var query = category.Where(row => row.CategoryId > 5)
                                .Select(row => row);
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

{“category_id”: 6, “category_name”: “XML 教程”}
{“category_id”: 7, “category_name”: “ASP.NET”}
{“category_id”: 8, “category_name”: “Web Service”}
{“category_id”: 9, “category_name”: “开发工具”}
{“category_id”: 10, “category_name”: “网站建设”}

上述代码是查询CategoryId > 5的结果

using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var query = category.Where(row => row.CategoryId > 5 && row.CategoryId < 8)
                                .Select(row => row);
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

{“category_id”: 6, “category_name”: “XML 教程”}
{“category_id”: 7, “category_name”: “ASP.NET”}

上述代码是查询CategoryId > 5并且CategoryId < 8的结果

using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var query = catalog.Where((row, index) => row.CatalogName.StartsWith("JavaScript") && index % 2 != 0)
                               .Select(row => new { row.CatalogId, row.CatalogName });
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

{ CatalogId = 388, CatalogName = JavaScript 教程 }
{ CatalogId = 390, CatalogName = JavaScript 用法 }
{ CatalogId = 392, CatalogName = JavaScript 语法 }
{ CatalogId = 394, CatalogName = JavaScript 注释 }
{ CatalogId = 396, CatalogName = JavaScript 数据类型 }
{ CatalogId = 398, CatalogName = JavaScript 函数 }
{ CatalogId = 400, CatalogName = JavaScript 事件 }
{ CatalogId = 402, CatalogName = JavaScript 运算符 }
{ CatalogId = 404, CatalogName = JavaScript 条件语句 }
{ CatalogId = 406, CatalogName = JavaScript for 循环 }
{ CatalogId = 408, CatalogName = JavaScript break 和 continue 语句 }
{ CatalogId = 410, CatalogName = JavaScript 类型转换 }
{ CatalogId = 412, CatalogName = JavaScript 错误 }
{ CatalogId = 414, CatalogName = JavaScript 变量提升 }
{ CatalogId = 416, CatalogName = JavaScript 使用误区 }
{ CatalogId = 418, CatalogName = JavaScript 表单验证 }
{ CatalogId = 420, CatalogName = JavaScript 保留关键字 }
{ CatalogId = 422, CatalogName = JavaScript let 和 const }
{ CatalogId = 424, CatalogName = JavaScript void }
{ CatalogId = 426, CatalogName = JavaScript Promise }
{ CatalogId = 428, CatalogName = JavaScript 函数定义 }
{ CatalogId = 430, CatalogName = JavaScript 函数调用 }
{ CatalogId = 440, CatalogName = JavaScript 对象 }
{ CatalogId = 442, CatalogName = JavaScript Number 对象 }
{ CatalogId = 444, CatalogName = JavaScript Date(日期) }
{ CatalogId = 446, CatalogName = JavaScript Boolean(布尔) }
{ CatalogId = 448, CatalogName = JavaScript RegExp 对象 }
{ CatalogId = 450, CatalogName = JavaScript Window Screen }
{ CatalogId = 452, CatalogName = JavaScript Window History }
{ CatalogId = 454, CatalogName = JavaScript 弹窗 }
{ CatalogId = 456, CatalogName = JavaScript Cookie }
{ CatalogId = 458, CatalogName = JavaScript 测试 jQuery }
{ CatalogId = 460, CatalogName = JavaScript 实例 }
{ CatalogId = 462, CatalogName = JavaScript 浏览器对象实例 }
{ CatalogId = 464, CatalogName = JavaScript 总结 }

上述代码查询的是JavaScript开头且索引为奇数的结果并只返回两个字段,因为CatalogId1开始,所以奇偶与索引相反。

排序

using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var query = catalog
                .Where(row => row.CatalogName.StartsWith("JavaScript") && row.CatalogId % 3 == 0)
                .OrderByDescending(row => row.CatalogId)
                .Select(row => new { row.CatalogId, row.CourseId, row.CatalogName });
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

{ CatalogId = 2715, CourseId = 89, CatalogName = JavaScript 指南 }
{ CatalogId = 465, CourseId = 9, CatalogName = JavaScript 对象 }
{ CatalogId = 462, CourseId = 9, CatalogName = JavaScript 浏览器对象实例 }
{ CatalogId = 459, CourseId = 9, CatalogName = JavaScript 测试 Prototype }
{ CatalogId = 456, CourseId = 9, CatalogName = JavaScript Cookie }
{ CatalogId = 453, CourseId = 9, CatalogName = JavaScript Navigator }
{ CatalogId = 450, CourseId = 9, CatalogName = JavaScript Window Screen }
{ CatalogId = 447, CourseId = 9, CatalogName = JavaScript Math(算数) }
{ CatalogId = 444, CourseId = 9, CatalogName = JavaScript Date(日期) }
{ CatalogId = 441, CourseId = 9, CatalogName = JavaScript prototype }
{ CatalogId = 429, CourseId = 9, CatalogName = JavaScript 函数参数 }
{ CatalogId = 426, CourseId = 9, CatalogName = JavaScript Promise }
{ CatalogId = 423, CourseId = 9, CatalogName = JavaScript JSON }
{ CatalogId = 420, CourseId = 9, CatalogName = JavaScript 保留关键字 }
{ CatalogId = 417, CourseId = 9, CatalogName = JavaScript 表单 }
{ CatalogId = 414, CourseId = 9, CatalogName = JavaScript 变量提升 }
{ CatalogId = 411, CourseId = 9, CatalogName = JavaScript 正则表达式 }
{ CatalogId = 408, CourseId = 9, CatalogName = JavaScript break 和 continue 语句 }
{ CatalogId = 405, CourseId = 9, CatalogName = JavaScript switch 语句 }
{ CatalogId = 402, CourseId = 9, CatalogName = JavaScript 运算符 }
{ CatalogId = 399, CourseId = 9, CatalogName = JavaScript 作用域 }
{ CatalogId = 396, CourseId = 9, CatalogName = JavaScript 数据类型 }
{ CatalogId = 393, CourseId = 9, CatalogName = JavaScript 语句 }
{ CatalogId = 390, CourseId = 9, CatalogName = JavaScript 用法 }

using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var query = catalog
                .Where(row => row.CatalogName.StartsWith("JavaScript") && row.CatalogId % 3 == 0)
                .OrderBy(row => row.CourseId)
                .ThenByDescending(row => row.CatalogId)
                .Select(row => new { row.CatalogId, row.CourseId, row.CatalogName });
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}


{ CatalogId = 465, CourseId = 9, CatalogName = JavaScript 对象 }
{ CatalogId = 462, CourseId = 9, CatalogName = JavaScript 浏览器对象实例 }
{ CatalogId = 459, CourseId = 9, CatalogName = JavaScript 测试 Prototype }
{ CatalogId = 456, CourseId = 9, CatalogName = JavaScript Cookie }
{ CatalogId = 453, CourseId = 9, CatalogName = JavaScript Navigator }
{ CatalogId = 450, CourseId = 9, CatalogName = JavaScript Window Screen }
{ CatalogId = 447, CourseId = 9, CatalogName = JavaScript Math(算数) }
{ CatalogId = 444, CourseId = 9, CatalogName = JavaScript Date(日期) }
{ CatalogId = 441, CourseId = 9, CatalogName = JavaScript prototype }
{ CatalogId = 429, CourseId = 9, CatalogName = JavaScript 函数参数 }
{ CatalogId = 426, CourseId = 9, CatalogName = JavaScript Promise }
{ CatalogId = 423, CourseId = 9, CatalogName = JavaScript JSON }
{ CatalogId = 420, CourseId = 9, CatalogName = JavaScript 保留关键字 }
{ CatalogId = 417, CourseId = 9, CatalogName = JavaScript 表单 }
{ CatalogId = 414, CourseId = 9, CatalogName = JavaScript 变量提升 }
{ CatalogId = 411, CourseId = 9, CatalogName = JavaScript 正则表达式 }
{ CatalogId = 408, CourseId = 9, CatalogName = JavaScript break 和 continue 语句 }
{ CatalogId = 405, CourseId = 9, CatalogName = JavaScript switch 语句 }
{ CatalogId = 402, CourseId = 9, CatalogName = JavaScript 运算符 }
{ CatalogId = 399, CourseId = 9, CatalogName = JavaScript 作用域 }
{ CatalogId = 396, CourseId = 9, CatalogName = JavaScript 数据类型 }
{ CatalogId = 393, CourseId = 9, CatalogName = JavaScript 语句 }
{ CatalogId = 390, CourseId = 9, CatalogName = JavaScript 用法 }
{ CatalogId = 2715, CourseId = 89, CatalogName = JavaScript 指南 }

分组

using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var query = course
                .GroupBy(row => row.CategoryId)
                .OrderBy(group => group.Count())
                .ThenBy(group => group.Key)
                .Select(group => new { Count = group.Count(), group.Key });
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

{ Count = 4, Key = 9 }
{ Count = 5, Key = 5 }
{ Count = 5, Key = 8 }
{ Count = 6, Key = 7 }
{ Count = 7, Key = 4 }
{ Count = 7, Key = 10 }
{ Count = 8, Key = 1 }
{ Count = 11, Key = 6 }
{ Count = 16, Key = 2 }
{ Count = 25, Key = 3 }

自定义变量

using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var query = course
                .GroupBy(row => row.CategoryId)
                .Select(group => new { Query = group, Count = group.Count(), group.Key })
                .OrderBy(group => group.Count)
                .ThenBy(group => group.Key)
                .Select(group => group);
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

{ Query = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course], Count = 4, Key = 9 }
{ Query = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course], Count = 5, Key = 5 }
{ Query = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course], Count = 5, Key = 8 }
{ Query = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course], Count = 6, Key = 7 }
{ Query = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course], Count = 7, Key = 4 }
{ Query = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course], Count = 7, Key = 10 }
{ Query = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course], Count = 8, Key = 1 }
{ Query = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course], Count = 11, Key = 6 }
{ Query = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course], Count = 16, Key = 2 }
{ Query = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course], Count = 25, Key = 3 }

内连接

using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var query = category
                .Join(course, row1 => row1.CategoryId, row2 => row2.CategoryId, (row1, row2) => new {
                    row1.CategoryName,
                    row2.CourseName,
                    row2.CourseIntro
                })
                .Where(row => row.CategoryName.StartsWith("ASP"))
                .Select(row => row);
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

{ CategoryName = ASP.NET, CourseName = 【学习 ASP.NET】, CourseIntro = ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架 }
{ CategoryName = ASP.NET, CourseName = 【学习 C#】, CourseIntro = C# 是一个简单的、现代的、通用的、面向对象的编程语言 }
{ CategoryName = ASP.NET, CourseName = 【学习 Web Pages】, CourseIntro = Web Pages 是三种网页编程模型中的一种,用于创建网站和web 应用程序 }
{ CategoryName = ASP.NET, CourseName = 【学习 Razor】, CourseIntro = Razor 是一种标记语法,可以让您将基于服务器的代码(Visual Basic 和 C#)嵌入到网页中 }
{ CategoryName = ASP.NET, CourseName = 【学习 MVC】, CourseIntro = MVC(Model View Controller 模型-视图-控制器) }
{ CategoryName = ASP.NET, CourseName = 【学习 Web Forms】, CourseIntro = Web Forms 是三种创建 ASP.NET 网站和 Web 应用程序的编程模式中的一种 }

左外连接

先跳过

组连接

using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var query = category
                .GroupJoin(course, row1 => row1.CategoryId, row2 => row2.CategoryId, (row1, row2) => new { Category = row1, CourseCount = row2.Count(), Course = row2 })
                .OrderBy(row => row.CourseCount)
                .Select(row => row);
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

{ Category = {“category_id”: 9, “category_name”: “开发工具”}, CourseCount = 4, Course = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course] }
{ Category = {“category_id”: 5, “category_name”: “移动端”}, CourseCount = 5, Course = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course] }
{ Category = {“category_id”: 8, “category_name”: “Web Service”}, CourseCount = 5, Course = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course] }
{ Category = {“category_id”: 7, “category_name”: “ASP.NET”}, CourseCount = 6, Course = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course] }
{ Category = {“category_id”: 4, “category_name”: “数据库”}, CourseCount = 7, Course = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course] }
{ Category = {“category_id”: 10, “category_name”: “网站建设”}, CourseCount = 7, Course = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course] }
{ Category = {“category_id”: 1, “category_name”: “HTML / CSS”}, CourseCount = 8, Course = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course] }
{ Category = {“category_id”: 6, “category_name”: “XML 教程”}, CourseCount = 11, Course = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course] }
{ Category = {“category_id”: 2, “category_name”: “JavaScript”}, CourseCount = 16, Course = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course] }
{ Category = {“category_id”: 3, “category_name”: “服务端”}, CourseCount = 25, Course = System.Linq.Lookup’2+Grouping[System.Int32,LINQcsdn.Course] }

集合操作

Distinct() Union() Intersect() Except() Zip() 这些是集合操作

去重Disinct()

using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var newSet = category.Concat(category).ToList();
            WriteLine(newSet.Count());
            var query = newSet.Distinct();
            WriteLine(query.Count());
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

20
10
{“category_id”: 1, “category_name”: “HTML / CSS”}
{“category_id”: 2, “category_name”: “JavaScript”}
{“category_id”: 3, “category_name”: “服务端”}
{“category_id”: 4, “category_name”: “数据库”}
{“category_id”: 5, “category_name”: “移动端”}
{“category_id”: 6, “category_name”: “XML 教程”}
{“category_id”: 7, “category_name”: “ASP.NET”}
{“category_id”: 8, “category_name”: “Web Service”}
{“category_id”: 9, “category_name”: “开发工具”}
{“category_id”: 10, “category_name”: “网站建设”}

交集Intersect()

using System.Collections.Generic;
using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var newSet1 = new List<Category>() { category[1], category[3], category[6] };
            var newSet2 = new List<Category>() { category[3], category[6], category[8] };
            Print(newSet1);
            Print(newSet2);
            var query = newSet1.Intersect(newSet2);
            Print(query);
        }

        public static void Print<T>(IEnumerable<T> query) {
            WriteLine("*=*=*=*=*=*=*=*=*=*=*=*=*");
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

============*
{“category_id”: 2, “category_name”: “JavaScript”}
{“category_id”: 4, “category_name”: “数据库”}
{“category_id”: 7, “category_name”: “ASP.NET”}
============*
{“category_id”: 4, “category_name”: “数据库”}
{“category_id”: 7, “category_name”: “ASP.NET”}
{“category_id”: 9, “category_name”: “开发工具”}
============*
{“category_id”: 4, “category_name”: “数据库”}
{“category_id”: 7, “category_name”: “ASP.NET”}

并集Union()

using System.Collections.Generic;
using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var newSet1 = new List<Category>() { category[1], category[3], category[6] };
            var newSet2 = new List<Category>() { category[3], category[6], category[8] };
            Print(newSet1);
            Print(newSet2);
            var query = newSet1.Union(newSet2);
            Print(query);
        }

        public static void Print<T>(IEnumerable<T> query) {
            WriteLine("*=*=*=*=*=*=*=*=*=*=*=*=*");
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

============*
{“category_id”: 2, “category_name”: “JavaScript”}
{“category_id”: 4, “category_name”: “数据库”}
{“category_id”: 7, “category_name”: “ASP.NET”}
============*
{“category_id”: 4, “category_name”: “数据库”}
{“category_id”: 7, “category_name”: “ASP.NET”}
{“category_id”: 9, “category_name”: “开发工具”}
============*
{“category_id”: 2, “category_name”: “JavaScript”}
{“category_id”: 4, “category_name”: “数据库”}
{“category_id”: 7, “category_name”: “ASP.NET”}
{“category_id”: 9, “category_name”: “开发工具”}

补集Except()

using System.Collections.Generic;
using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var newSet1 = new List<Category>() { category[1], category[3], category[6] };
            var newSet2 = new List<Category>() { category[3], category[6], category[8] };
            Print(newSet1);
            Print(newSet2);
            var query = newSet1.Except(newSet2);
            Print(query);
            query = newSet2.Except(newSet1);
            Print(query);
        }

        public static void Print<T>(IEnumerable<T> query) {
            WriteLine("*=*=*=*=*=*=*=*=*=*=*=*=*");
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

============*
{“category_id”: 2, “category_name”: “JavaScript”}
{“category_id”: 4, “category_name”: “数据库”}
{“category_id”: 7, “category_name”: “ASP.NET”}
============*
{“category_id”: 4, “category_name”: “数据库”}
{“category_id”: 7, “category_name”: “ASP.NET”}
{“category_id”: 9, “category_name”: “开发工具”}
============*
{“category_id”: 2, “category_name”: “JavaScript”}
============*
{“category_id”: 9, “category_name”: “开发工具”}

合并Zip()

using System.Collections.Generic;
using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            var query = category.Zip(course, (row1, row2) => new { row1.CategoryName, row2.CourseName, row2.CourseIntro });
            Print(query);
        }

        public static void Print<T>(IEnumerable<T> query) {
            WriteLine("*=*=*=*=*=*=*=*=*=*=*=*=*");
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

============*
{ CategoryName = HTML / CSS, CourseName = 【学习 HTML】, CourseIntro = HTML,即超文本标记语言(Hyper Text Markup Language) }
{ CategoryName = JavaScript, CourseName = 【学习 HTML5】, CourseIntro = HTML5 是下一代 HTML 标准 }
{ CategoryName = 服务端, CourseName = 【学习 CSS】, CourseIntro = 层叠样式表(Cascading StyleSheet) }
{ CategoryName = 数据库, CourseName = 【学习 CSS3】, CourseIntro = CSS3是CSS技术的升级版本 }
{ CategoryName = 移动端, CourseName = 【学习 Bootstrap3】, CourseIntro = Bootstrap,来自 Twitter,是目前最受欢迎的前端框架 }
{ CategoryName = XML 教程, CourseName = 【学习 Bootstrap4】, CourseIntro = Bootstrap4 目前是 Bootstrap 的最新版本 }
{ CategoryName = ASP.NET, CourseName = 【学习 Font Awesome】, CourseIntro = Font Awesome 是一套绝佳的图标字体库和CSS框架。 }
{ CategoryName = Web Service, CourseName = 【学习 Foundation】, CourseIntro = Foundation 用于开发响应式的 HTML, CSS and JavaScript 框架 }
{ CategoryName = 开发工具, CourseName = 【学习 JavaScript】, CourseIntro = JavaScript 是 Web 的编程语言 }
{ CategoryName = 网站建设, CourseName = 【学习 HTML DOM】, CourseIntro = HTML DOM 定义了访问和操作 HTML 文档的标准方法 }

分页

通过Skip()Take()完成,前者是忽略的数量,后者是获取的数量。

using System.Collections.Generic;
using System.Linq;
using static System.Console;

namespace LINQcsdn {
    class Program {
        static void Main(string[] args) {
            var category = Category.GetList();
            var course = Course.GetList();
            var catalog = Catalog.GetList();
            int page = 3, limit = 10;
            var query = catalog
                .OrderBy(row => row.CatalogId)
                .Select(row => row)
                .Skip((page - 1) * limit).Take(limit);
            Print(query);
        }

        public static void Print<T>(IEnumerable<T> query) {
            WriteLine("*=*=*=*=*=*=*=*=*=*=*=*=*");
            foreach (var item in query) {
                WriteLine(item);
            }
        }
    }
}

============*
{“catalog_id”: 21, “course_id”: 1, “catalog_name”: “HTML 颜色名”, “catalog_url”: “https://www.runoob.com/html/html-colornames.html”}
{“catalog_id”: 22, “course_id”: 1, “catalog_name”: “HTML 颜色值”, “catalog_url”: “https://www.runoob.com/html/html-colorvalues.html”}
{“catalog_id”: 23, “course_id”: 1, “catalog_name”: “HTML 脚本”, “catalog_url”: “https://www.runoob.com/html/html-scripts.html”}
{“catalog_id”: 24, “course_id”: 1, “catalog_name”: “HTML 字符实体”, “catalog_url”: “https://www.runoob.com/html/html-entities.html”}
{“catalog_id”: 25, “course_id”: 1, “catalog_name”: “HTML URL”, “catalog_url”: “https://www.runoob.com/html/html-url.html”}
{“catalog_id”: 26, “course_id”: 1, “catalog_name”: “HTML 速查列表”, “catalog_url”: “https://www.runoob.com/html/html-quicklist.html”}
{“catalog_id”: 27, “course_id”: 1, “catalog_name”: “HTML 总结”, “catalog_url”: “https://www.runoob.com/html/html-summary.html”}
{“catalog_id”: 28, “course_id”: 1, “catalog_name”: “XHTML 简介”, “catalog_url”: “https://www.runoob.com/html/html-xhtml.html”}
{“catalog_id”: 29, “course_id”: 1, “catalog_name”: “HTML5 教程”, “catalog_url”: “https://www.runoob.com/html/html5-intro.html”}
{“catalog_id”: 30, “course_id”: 1, “catalog_name”: “HTML5 浏览器支持”, “catalog_url”: “https://www.runoob.comhtml5-browsers.html”}

小结

  • 一些基本操作

你可能感兴趣的:(#,CSharp,c#,linq)