LINQ查询表达式和LAMBDA点标记方法基础

在上一篇文章中,我们介绍了LINQ的一些基本用法,这一篇我们来看下另一种更简洁更优雅的表达式,Lambda表达式,也可以叫做点标记方法。

相信大家在实际工作中都用到过这两种方式,下面我们还是用实例来看下两种表达式的一些基本用法。

首先还是创建一个C#控制台程序,准备测试数据,创建两个类,Product产品类和Category类别类,每个产品分配一个类别。

 1     public class Category
 2     {
 3         public int CategoryID { get; set; }
 4         public string CategoryName { get; set; }
 5     }
 6 
 7     public class Product
 8     {
 9         public int ProductID { get; set; }
10         public string ProductName { get; set; }
11         public int CategoryID { get; set; }
12         public int Count { get; set; }
13         public string Description { get; set; }
14     }

接着创建一些Category类型和Product类型的测试数组列表。

 1             Category[] categories = new Category[]{
 2                 new Category{CategoryID=1,CategoryName="TOYOTA"},
 3                 new Category{CategoryID=2,CategoryName="BMW"},
 4                 new Category{CategoryID=3,CategoryName="BENZ"},
 5                 new Category{CategoryID=4,CategoryName="HONDA"},
 6                 new Category{CategoryID=5,CategoryName="LEXUS"}
 7             };
 8 
 9             Product[] products = new Product[]{
10                 new Product{ProductID=1,ProductName="Car1",CategoryID=1,Count=5,Description="TOYOTA Car"},
11                 new Product{ProductID=2,ProductName="Car2",CategoryID=1,Count=8,Description="TOYOTA SUV"},
12                 new Product{ProductID=3,ProductName="Car3",CategoryID=2,Count=2,Description="BMW Car"},
13                 new Product{ProductID=4,ProductName="Car4",CategoryID=2,Count=3,Description="BMW SUV"},
14                 new Product{ProductID=5,ProductName="Car5",CategoryID=3,Count=5,Description="BENZ Car"},
15                 new Product{ProductID=6,ProductName="Car6",CategoryID=4,Count=10,Description="HONDA Car"},
16                 new Product{ProductID=7,ProductName="Car7",CategoryID=5,Count=6,Description="LEXUS Car"},
17                 new Product{ProductID=8,ProductName="Car8",CategoryID=5,Count=2,Description="LEXUS SUV"}
18             };

1.查询产品列表中所有记录的ProductName,Count,Description列。(select)

 1             // LINQ
 2             var productQuery1 = from p in products
 3                                select new
 4                                {
 5                                    p.ProductName,
 6                                    p.Count,
 7                                    p.Description
 8                                };
 9 
10             Console.WriteLine("LINQ---");
11             foreach(var item in productQuery1)
12             {
13                 Console.WriteLine("Product Name: {0} , Count: {1} , Description: {2}", item.ProductName, item.Count, item.Description);
14             }
15 
16             // LAMBDA
17             var productQuery2 = products.Select(p => new
18             {
19                 ProductName = p.ProductName,
20                 Count = p.Count,
21                 Description = p.Description
22             });
23 
24             Console.WriteLine("LAMBDA---");
25             foreach (var item in productQuery2)
26             {
27                 Console.WriteLine("Product Name: {0} , Count: {1} , Description: {2}", item.ProductName, item.Count, item.Description);
28             }

 

2.查询产品列表中Count大于5的记录。(where)

 1             // LINQ
 2             var productQuery1 = from p in products
 3                                 where p.Count > 5
 4                                 select p;
 5 
 6             Console.WriteLine("LINQ---");
 7             foreach (var item in productQuery1)
 8             {
 9                 Console.WriteLine(item.ProductName);
10             }
11 
12             // LAMBDA
13             var productQuery2 = products.Where(P => P.Count > 5);
14 
15             Console.WriteLine("LAMBDA---");
16             foreach (var item in productQuery2)
17             {
18                 Console.WriteLine(item.ProductName);
19             }

3.查找产品列表中类别为TOYOTA,BENZ,LEXUS的记录。(where / contains)

 1             // LINQ
 2             var productQuery1 = from p in products
 3                                 where (new int[] { 1,3,5}).Contains(p.CategoryID)
 4                                 select p;
 5 
 6             Console.WriteLine("LINQ---");
 7             foreach (var item in productQuery1)
 8             {
 9                 Console.WriteLine("Product Name: {0} , Category ID: {1} , Description: {2}", item.ProductName, item.CategoryID, item.Description);
10             }
11 
12             // LAMBDA
13             var productQuery2 = products.Where(p => new int[] { 1, 3, 5 }.Contains(p.CategoryID));
14 
15             Console.WriteLine("LAMBDA---");
16             foreach (var item in productQuery2)
17             {
18                 Console.WriteLine("Product Name: {0} , Category ID: {1} , Description: {2}", item.ProductName, item.CategoryID, item.Description);
19             }

4.查找产品列表中类别为TOYOTA,LEXUS的记录,并且结果按产品数量Count降序排列。(where / orderby)

 1             // LINQ
 2             var productQuery1 = from p in products
 3                                 where p.CategoryID==1 || p.CategoryID==5
 4                                 orderby p.Count descending
 5                                 select p;
 6 
 7             Console.WriteLine("LINQ---");
 8             foreach (var item in productQuery1)
 9             {
10                 Console.WriteLine("Product Name: {0} , Category ID: {1} , Count: {2} , Description: {3}", item.ProductName, item.CategoryID, item.Count,item.Description);
11             }
12 
13             // LAMBDA
14             var productQuery2 = products.Where(p => (p.CategoryID == 1 || p.CategoryID == 5)).OrderByDescending(p => p.Count);
15 
16             Console.WriteLine("LAMBDA---");
17             foreach (var item in productQuery2)
18             {
19                 Console.WriteLine("Product Name: {0} , Category ID: {1} , Count: {2} , Description: {3}", item.ProductName, item.CategoryID, item.Count, item.Description);
20             }

5.产品列表和类别列表联合查询。(join)

 1             // LINQ
 2             var productJoinCategoryQuery1 = from p in products
 3                                 join c in categories
 4                                 on p.CategoryID equals c.CategoryID
 5                                 select new { p, c };
 6                                
 7             Console.WriteLine("LINQ---");
 8             foreach (var item in productJoinCategoryQuery1)
 9             {
10                 Console.WriteLine("Product Name: {0} , Category Name: {1} , Count: {2} , Description: {3}", item.p.ProductName, item.c.CategoryName, item.p.Count, item.p.Description);
11             }
12 
13             // LAMBDA
14             var productJoinCategoryQuery2 = products.Join(categories,
15                 p => p.CategoryID,
16                 c => c.CategoryID,
17                 (p, c) => new { p, c });
18 
19             Console.WriteLine("LAMBDA---");
20             foreach (var item in productJoinCategoryQuery2)
21             {
22                 Console.WriteLine("Product Name: {0} , Category Name: {1} , Count: {2} , Description: {3}", item.p.ProductName, item.c.CategoryName, item.p.Count, item.p.Description);
23             }

6.产品列表和类别列表分组联合查询。(join & group)

 1             // LINQ
 2             var productByCategoryResult1 = from c in categories
 3                                           join p in products
 4                                           on c.CategoryID equals p.CategoryID
 5                                           into CategoryProducts
 6                                           select new
 7                                           {
 8                                               c.CategoryName,
 9                                               Products = CategoryProducts
10                                           };
11                               
12             Console.WriteLine("LINQ---");
13             foreach (var item in productByCategoryResult1)
14             {
15                 Console.WriteLine("Category: {0}", item.CategoryName);
16                 foreach (var product in item.Products)
17                 {
18                     Console.WriteLine("{0} ({1})", product.ProductName, product.Description);
19                 }
20                 Console.WriteLine();
21             }
22 
23             // LAMBDA
24             var productByCategoryResult2 = categories.GroupJoin(products,
25                 c => c.CategoryID,
26                 p => p.CategoryID,
27                 (c, p) => new { c.CategoryName, Products = p });
28 
29             Console.WriteLine("LAMBDA---");
30             foreach (var item in productByCategoryResult2)
31             {
32                 Console.WriteLine("Category: {0}", item.CategoryName);
33                 foreach (var product in item.Products)
34                 {
35                     Console.WriteLine("{0} ({1})", product.ProductName, product.Description);
36                 }
37                 Console.WriteLine();
38             }

7.LAMBDA的一些其他用法。

 1             // LAMBDA 一些其他常用方法
 2 
 3             // Min
 4             var productQueryMin = products.Min(p => p.Count);
 5             Console.WriteLine("Count Min: {0}", productQueryMin);
 6             
 7             // Max
 8             var productQueryMax = products.Max(p => p.Count);
 9             Console.WriteLine("Count Max: {0}", productQueryMax);
10             
11             // Average
12             var productQueryAverage = products.Average(p => p.Count);
13             Console.WriteLine("Count Average: {0}", productQueryAverage);
14             
15             // Sum
16             var productQuerySum= products.Sum(p => p.Count);
17             Console.WriteLine("Count Sum: {0}", productQuerySum);
18 
19             // Skip / Take
20             var productQueryPage = products.Skip(3).Take(3).Select(p => new { p.ProductName, p.Count, p.Description });
21             foreach (var item in productQueryPage)
22             {
23                 Console.WriteLine("Product Name: {0} , Count: {1} , Description: {2}", item.ProductName, item.Count, item.Description);
24             }
25 
26             // First (返回第一条记录,如果没有记录,则抛出异常)
27             var firstProduct = products.First();
28 
29             // 此处查询的记录不存在,所以这行代码会抛出异常
30             var firstProductNull = products.Where(p => p.Count > 100).First();
31 
32             // FirstOrDefault (返回第一条记录,如果没有,则返回null)
33             var firstOrDefaultProduct = products.FirstOrDefault();
34 
35             // 此处查询的记录不存在,所以这行代码返回null
36             var firstOrDefaultProductNull = products.Where(p => p.Count > 100).FirstOrDefault();
37 
38             // Single (返回集合中唯一的记录,如果大于或小于一条记录,则抛出异常,这行代码执行记录大于一条,所以会抛出异常)
39             var singleProduct = products.Single();
40 
41             // 这行代码执行记录小于一条记录,所以也会抛出异常
42             var singleProductNull = products.Where(p => p.Count > 100).SingleOrDefault();

 

 

好了,本篇就先到此,希望对你有所帮助,谢谢!

 

posted @ 2017-02-17 18:20  MeJoy  阅读(1022)  评论(1编辑  收藏  举报