本篇文章我们将演示LINQ扩展包语法里的投影特性,用投影实现LINQ结果集的类型转换。目前LINQ支持两种语法,我会在每个案例前先用大家熟知的SQL语句表达,再在后面用C#的两种LINQ语法分别实现。LINQ语法第一次接触难免感到陌生,最好的学习方式就是在项目中多去使用,相信会有很多感悟。
在学习之前,我们要做一些准备工作,我们需要创建User对象和包含User对象的集合,作为后面查询和输出的数据源。
C#类:
class User
{
public int id { get; set; }
public string name { get; set; }
public bool gender { get; set; }//male: true; female: fasle
public int age { get; set; }
public string occupation { get; set; } //职业
}
List<User> list = new List<User>()
{
new User { id = 1, name = "Zhang Long", age = 38, gender = true, occupation = "Teacher"},
new User { id = 2, name = "Zhang Jin", age = 18, gender = false, occupation = "Student"},
new User { id = 3, name = "Zhang Shuai", age = 38, gender = false, occupation = "Teacher"},
new User { id = 4, name = "Liu Guangzhi", age = 38, gender = false, occupation = "Doctor"},
new User { id = 5, name = "Liu Ziming", age = 38, gender = true, occupation = "Doctor"},
new User { id = 6, name = "Liu Shuai", age = 29, gender = false, occupation = "Doctor"},
new User { id = 7, name = "Liu Jin", age = 21, gender = true, occupation = "Builder"},
new User { id = 8, name = "Jiang Long", age = 38, gender = true, occupation = "Builder"},
new User { id = 9, name = "Hu Ziming", age = 21, gender = true, occupation = "Student"},
new User { id = 10, name = "Hu Jin", age = 21, gender = false, occupation = "Student"}
};
数据源1:
数据源2:
投影的过程就是把取得的结果进行处理,可以把结果集合内的对象只取其中一个或多个元素组成一个新的集合,生成一个原对象、基本类型、元组或匿名对象的新集合。
/* C#版本1 */
List<User> userList = (from u in list where u.occupation == "Doctor" select u).ToList();
/* C#版本2 */
List<User> userList = list.Where(p => p.occupation == "Doctor").ToList();
/* 投影后集合 */
[{id = 11, name = Zhang San, age = 21, gender = False, occupation = Doctor},
{id = 7, name = Li Si, age = 21, gender = True, occupation = Doctor},
{id = 2, name = Wang Wu, age = 18, gender = False, occupation = Doctor}]
这里是把原有的User集合中每个User的name属性提取出来,组成一个全是name的新的集合,故新集合为String属性。
/* C#版本1 */
List<string> nameList = (from u in list where u.occupation == "Doctor" select u.name).ToList();
/* C#版本2 */
List<string> nameList = list.Where(p => p.occupation == "Doctor").Select(x => x.name).ToList();
/* 投影后集合 */
["Zhang San", "Li Si", "Wang Wu"]
/* C#版本1 */
var userList = (from u in list where u.occupation == "Doctor" select ( Name: u.name, Age: u.age )).ToList();
/* C#版本2 */
var userList = list.Where(x => x.occupation == "Doctor").Select(u => (Name: u.name, Age: u.age)).ToList();
/* 投影后集合 */
[("Zhang San", 21), ("Li Si", 21), ("Wang Wu", 18)]
/* C#版本1 */
var userList = (from u in list where u.occupation == "Doctor" select new { u.name, u.age }).ToList();
/* C#版本2 */
var userList = list.Where(x => x.occupation == "Doctor").Select(u => new { u.name, u.age }).ToList();
/* 投影后集合 */
[{"Zhang San", 21}, {"Li Si", 21}, {"Wang Wu", 18}]
在LINQ(Language Integrated Query)中,投影是一种强大的数据转换技术,它允许开发者从数据源中提取所需的特定字段或属性,生成新的数据结构。投影常用于从复杂的数据集中创建简化的视图,或者组合和修改数据元素以形成新的集合。这一功能提高了查询的灵活性和数据处理的效率。
投影的使用场景:
投影是LINQ中一个极其有用的功能,它通过允许开发者自定义数据的选择和处理,极大地提升了代码的灵活性和数据操作的效率。在开发过程中合理利用投影,不仅可以简化数据处理过程,还可以为应用程序提供更加精细和高效的数据处理能力。无论是在数据分析、报表生成还是日常的数据处理中,投影都是实现高效和灵活数据操作的关键技术。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。