首页 > 代码库 > LINQ 101——分区、Join、聚合

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 }
View Code

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 }
View Code

Take + Skip 实现分页

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

 

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 Product19 {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 }
View Code

 

例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 }
View Code

例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 tgroup10                 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 }
View Code

例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 }
View Code

 

3. 聚合

Count();

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

Sum();

Min()

Max()

Average()

 

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

 

LINQ 101——分区、Join、聚合