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

时间:2023-03-08 17:02:31
LINQ查询表达式和LAMBDA点标记方法基础

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

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

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

     public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
} public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int CategoryID { get; set; }
public int Count { get; set; }
public string Description { get; set; }
}

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

             Category[] categories = new Category[]{
new Category{CategoryID=,CategoryName="TOYOTA"},
new Category{CategoryID=,CategoryName="BMW"},
new Category{CategoryID=,CategoryName="BENZ"},
new Category{CategoryID=,CategoryName="HONDA"},
new Category{CategoryID=,CategoryName="LEXUS"}
}; Product[] products = new Product[]{
new Product{ProductID=,ProductName="Car1",CategoryID=,Count=,Description="TOYOTA Car"},
new Product{ProductID=,ProductName="Car2",CategoryID=,Count=,Description="TOYOTA SUV"},
new Product{ProductID=,ProductName="Car3",CategoryID=,Count=,Description="BMW Car"},
new Product{ProductID=,ProductName="Car4",CategoryID=,Count=,Description="BMW SUV"},
new Product{ProductID=,ProductName="Car5",CategoryID=,Count=,Description="BENZ Car"},
new Product{ProductID=,ProductName="Car6",CategoryID=,Count=,Description="HONDA Car"},
new Product{ProductID=,ProductName="Car7",CategoryID=,Count=,Description="LEXUS Car"},
new Product{ProductID=,ProductName="Car8",CategoryID=,Count=,Description="LEXUS SUV"}
};

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

             // LINQ
var productQuery1 = from p in products
select new
{
p.ProductName,
p.Count,
p.Description
}; Console.WriteLine("LINQ---");
foreach(var item in productQuery1)
{
Console.WriteLine("Product Name: {0} , Count: {1} , Description: {2}", item.ProductName, item.Count, item.Description);
} // LAMBDA
var productQuery2 = products.Select(p => new
{
ProductName = p.ProductName,
Count = p.Count,
Description = p.Description
}); Console.WriteLine("LAMBDA---");
foreach (var item in productQuery2)
{
Console.WriteLine("Product Name: {0} , Count: {1} , Description: {2}", item.ProductName, item.Count, item.Description);
}

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

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

             // LINQ
var productQuery1 = from p in products
where p.Count >
select p; Console.WriteLine("LINQ---");
foreach (var item in productQuery1)
{
Console.WriteLine(item.ProductName);
} // LAMBDA
var productQuery2 = products.Where(P => P.Count > ); Console.WriteLine("LAMBDA---");
foreach (var item in productQuery2)
{
Console.WriteLine(item.ProductName);
}

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

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

             // LINQ
var productQuery1 = from p in products
where (new int[] { ,,}).Contains(p.CategoryID)
select p; Console.WriteLine("LINQ---");
foreach (var item in productQuery1)
{
Console.WriteLine("Product Name: {0} , Category ID: {1} , Description: {2}", item.ProductName, item.CategoryID, item.Description);
} // LAMBDA
var productQuery2 = products.Where(p => new int[] { , , }.Contains(p.CategoryID)); Console.WriteLine("LAMBDA---");
foreach (var item in productQuery2)
{
Console.WriteLine("Product Name: {0} , Category ID: {1} , Description: {2}", item.ProductName, item.CategoryID, item.Description);
}

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

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

             // LINQ
var productQuery1 = from p in products
where p.CategoryID== || p.CategoryID==
orderby p.Count descending
select p; Console.WriteLine("LINQ---");
foreach (var item in productQuery1)
{
Console.WriteLine("Product Name: {0} , Category ID: {1} , Count: {2} , Description: {3}", item.ProductName, item.CategoryID, item.Count,item.Description);
} // LAMBDA
var productQuery2 = products.Where(p => (p.CategoryID == || p.CategoryID == )).OrderByDescending(p => p.Count); Console.WriteLine("LAMBDA---");
foreach (var item in productQuery2)
{
Console.WriteLine("Product Name: {0} , Category ID: {1} , Count: {2} , Description: {3}", item.ProductName, item.CategoryID, item.Count, item.Description);
}

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

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

             // LINQ
var productJoinCategoryQuery1 = from p in products
join c in categories
on p.CategoryID equals c.CategoryID
select new { p, c }; Console.WriteLine("LINQ---");
foreach (var item in productJoinCategoryQuery1)
{
Console.WriteLine("Product Name: {0} , Category Name: {1} , Count: {2} , Description: {3}", item.p.ProductName, item.c.CategoryName, item.p.Count, item.p.Description);
} // LAMBDA
var productJoinCategoryQuery2 = products.Join(categories,
p => p.CategoryID,
c => c.CategoryID,
(p, c) => new { p, c }); Console.WriteLine("LAMBDA---");
foreach (var item in productJoinCategoryQuery2)
{
Console.WriteLine("Product Name: {0} , Category Name: {1} , Count: {2} , Description: {3}", item.p.ProductName, item.c.CategoryName, item.p.Count, item.p.Description);
}

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

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

             // LINQ
var productByCategoryResult1 = from c in categories
join p in products
on c.CategoryID equals p.CategoryID
into CategoryProducts
select new
{
c.CategoryName,
Products = CategoryProducts
}; Console.WriteLine("LINQ---");
foreach (var item in productByCategoryResult1)
{
Console.WriteLine("Category: {0}", item.CategoryName);
foreach (var product in item.Products)
{
Console.WriteLine("{0} ({1})", product.ProductName, product.Description);
}
Console.WriteLine();
} // LAMBDA
var productByCategoryResult2 = categories.GroupJoin(products,
c => c.CategoryID,
p => p.CategoryID,
(c, p) => new { c.CategoryName, Products = p }); Console.WriteLine("LAMBDA---");
foreach (var item in productByCategoryResult2)
{
Console.WriteLine("Category: {0}", item.CategoryName);
foreach (var product in item.Products)
{
Console.WriteLine("{0} ({1})", product.ProductName, product.Description);
}
Console.WriteLine();
}

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

7.LAMBDA的一些其他用法。

             // LAMBDA 一些其他常用方法

             // Min
var productQueryMin = products.Min(p => p.Count);
Console.WriteLine("Count Min: {0}", productQueryMin); // Max
var productQueryMax = products.Max(p => p.Count);
Console.WriteLine("Count Max: {0}", productQueryMax); // Average
var productQueryAverage = products.Average(p => p.Count);
Console.WriteLine("Count Average: {0}", productQueryAverage); // Sum
var productQuerySum= products.Sum(p => p.Count);
Console.WriteLine("Count Sum: {0}", productQuerySum); // Skip / Take
var productQueryPage = products.Skip().Take().Select(p => new { p.ProductName, p.Count, p.Description });
foreach (var item in productQueryPage)
{
Console.WriteLine("Product Name: {0} , Count: {1} , Description: {2}", item.ProductName, item.Count, item.Description);
} // First (返回第一条记录,如果没有记录,则抛出异常)
var firstProduct = products.First(); // 此处查询的记录不存在,所以这行代码会抛出异常
var firstProductNull = products.Where(p => p.Count > ).First(); // FirstOrDefault (返回第一条记录,如果没有,则返回null)
var firstOrDefaultProduct = products.FirstOrDefault(); // 此处查询的记录不存在,所以这行代码返回null
var firstOrDefaultProductNull = products.Where(p => p.Count > ).FirstOrDefault(); // Single (返回集合中唯一的记录,如果大于或小于一条记录,则抛出异常,这行代码执行记录大于一条,所以会抛出异常)
var singleProduct = products.Single(); // 这行代码执行记录小于一条记录,所以也会抛出异常
var singleProductNull = products.Where(p => p.Count > ).SingleOrDefault();

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