LINQ 101——分区、Join、聚合

1.Partitioning 分区

Take

例1:取前3个数

 1 static void Linq1()
 2 {
 3     int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
 4     var first3Numbers = numbers.Take(3);
 5     Console.WriteLine("前3个数:");
 6     foreach (var n in first3Numbers)
 7     {
 8         Console.WriteLine(n);
 9     }
10 }

Skip

例2:跳过前3个数

 1 static void Linq2()
 2 {
 3     int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
 4     var skip3Numbers = numbers.Skip(3);
 5     Console.WriteLine("除去前3个数后数字:");
 6     foreach (var n in skip3Numbers)
 7     {
 8         Console.WriteLine(n);
 9     }
10 }

Take + Skip 实现分页

1 int currentPageIndex;  // 当前页号
2 int pageSize;      // 每页有多少条记录
3 source 是总数据源集合
4
5 则用LINQ分页的代码为
6
7 var query = source.Skip((currentPageIndex-1) * pageSize).Take(pageSize);

2. Join

例1: Cross Join

 1 static void Linq3()
 2 {
 3     string[] cates = {"酒类","烟类","肉类" };
 4     var products = Product.GetDefaultData();
 5
 6     // CategoryName 没有与 cate 匹配的将被剔除
 7     var query = from c in cates
 8                 join p in products on c equals p.CategoryName
 9                 select new { ShowName = c + " : " + p.ProductName };
10
11     foreach (var item in query)
12     {
13         Console.WriteLine(item.ShowName);
14     }
15 }
16
17
18 public class Product
19 {
20     public string CategoryName { get; set; }
21     public string ProductName { get; set; }
22
23     public static List<Product> GetDefaultData()
24     {
25         return new List<Product>
26         {
27             new Product{ CategoryName="烟类", ProductName="中华"},
28             new Product{ CategoryName="酒类", ProductName="白酒"},
29             new Product{ CategoryName="酒类", ProductName="红酒"},
30             new Product{ CategoryName="肉类", ProductName="猪肉"},
31             new Product{ CategoryName="肉类", ProductName="牛肉"},
32             new Product{ CategoryName="零食", ProductName="饼干"},
33             new Product{ ProductName="暂未分类产品1"},
34             new Product{ ProductName="暂未分类产品2"},
35         };
36     }
37 }

例2: Group Join

 1 static void Linq4()
 2 {
 3     string[] cates = { "酒类", "烟类", "肉类" };
 4     var products = Product.GetDefaultData();
 5
 6     var query = from c in cates
 7                 join p in products on c equals p.CategoryName
 8                 into tgroup
 9                 select new { Name = c, Group = tgroup };
10
11     foreach (var item in query)
12     {
13         Console.Write("{0}: ",item.Name);
14         foreach (var g in item.Group)
15         {
16             Console.Write("{0} ",g.ProductName);
17         }
18         Console.WriteLine();
19     }
20 }

例3:Cross Join + Group Join

分组后再查询符合条件的

 1 static void Linq5()
 2 {
 3     string[] cates = { "酒类", "烟类", "肉类" };
 4     var products = Product.GetDefaultData();
 5
 6     var query = from c in cates
 7                 join p in products on c equals p.CategoryName
 8                 into tgroup
 9                 from g in tgroup
10                 where g.ProductName.Contains("牛腩")  // 仅要产品名字含有牛腩的分组
11                 select new { Name = c, Group = tgroup };
12
13     foreach (var item in query)
14     {
15         Console.Write("{0}: ", item.Name);
16         foreach (var g in item.Group)
17         {
18             Console.Write("{0} ", g.ProductName);
19         }
20         Console.WriteLine();
21     }
22 }

例4 Left Outer Join

 1 static void Linq6()
 2 {
 3     string[] cates = { "酒类", "烟类", "肉类" ,"调料类"};
 4     var products = Product.GetDefaultData();
 5
 6     var query = from c in cates
 7                 join p in products on c equals p.CategoryName
 8                 into tgroup
 9                 from g in tgroup.DefaultIfEmpty()
10                 select new { Name = c, ProductName = g==null ? "该分组下没有内容":g.ProductName };
11
12     foreach (var item in query)
13     {
14         Console.WriteLine("{0} {1}",item.Name,item.ProductName);
15     }
16 }

3. 聚合

Count();

Count(n => 条件); 后面都类似

Sum();

Min()

Max()

Average()

本文代码:http://files.cnblogs.com/Aphasia/ConsoleApplication2.rar

时间: 2024-12-28 16:08:43

LINQ 101——分区、Join、聚合的相关文章

LINQ 101——约束、投影、排序

什么是LINQ:LINQ 是一组 .NET Framework 扩展模块集合,内含语言集成查询.集合以及转换操作.它使用查询的本机语言语法来扩展 C# 和 Visual Basic,并提供利用这些功能的类库. 什么是LINQ 101:是学习LINQ的不错的资源(下载地址:http://pan.baidu.com/s/1ntE74NJ),当初我也是看LINQ101来熟悉LINQ的.现把我当时的学习笔记整理出来 Restriction (约束)Projection (投影)Ordering (排序)

LINQ 101——分组、Set、转换、Element

一.Grouping(分组) 例1:对于0-9数按被3整除的结果分组 代码: 1 static void Linq1() 2 { 3 int[] numbers = { 5, 3, 2, 4, 0, 7, 8, 6, 9, 1 }; 4 var numModBy3 = from n in numbers 5 group n by n % 3 6 into g 7 select new { Remainder = g.Key, Numbers = g }; 8 9 foreach (var g i

reduce端连接-分区分组聚合

1.1.1         reduce端连接-分区分组聚合 reduce端连接则是利用了reduce的分区功能将stationid相同的分到同一个分区,在利用reduce的分组聚合功能,将同一个stationid的气象站数据和温度记录数据分为一组,reduce函数读取分组后的第一个记录(就是气象站的名称)与其他记录组合后输出,实现连接.例如连接下面气象站数据集和温度记录数据集.先用几条数据做分析说明,实际肯定不只这点数据. 气象站数据集,气象站id和名称数据表 StationId Statio

Linq中eft join之大坑

1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Data; 5 using System.IO; 6 using System.Linq; 7 using Newtonsoft.Json; 8 9 namespace CLibrary.ConsoleApp 10 { 11 class Program 12 { 13 static void Main(str

LINQ系列:LINQ to SQL Join连接

1. 一对多 var expr = context.Products .Where(p => p.Category.CategoryName == "LINQ to SQL" && p.UnitPrice > 10m) .Select(p => new { p.ProductID, p.ProductName }); var expr = from p in context.Products where p.Category.CategoryName

数据库和linq中的 join(连接)操作

sql中的连接 sql中的表连接有inner join,left join(left outer join),right join(right outer join),full join(full outer join),cross join 在此基础上我们能扩展出 left excluding join,right excluding join,full outer excluding join 注:left join是left outer join 的简写,即左连接和左外连接是一样的 首先定

linq多表join与group

var query =from a in this.ObjectContext.siteInfo join b in this.ObjectContext.shopInfo on a.siteID equals b.siteID group new {a,b} by new { a.Lon, a.Lat, a.siteID, b.date} into g select new site_shopInfo{ SiteID=g.Key.siteID, Longitude=g.Key.Lon, Lat

linq 多条件join

var query=from a in db.A           join b in db.B.Where(c=>c.num>3)             on new {a.type,a.item} equals new {b.type,b.item} into g           from b in g.DefaultIfEmpty()           select new           {               a,              num=b==nul

Linq学习(join)

<span style="font-size:14px;"> //学生类 public class Stu { public int ID { set; get; } //学生的编号 public string Name { set; get; } //学生的姓名 public int CourseId { set; get; } //学生所选课程的id } //课程类 public class Course { public int ID { set; get; } //