详解如何在C#中使用投影(Projection)
- 作者: 晕逼的男人
- 来源: 51数据库
- 2021-07-30
投影(projection) 是一种可以将查询结果进行 塑性 的一种操作,你可以使用 投影 将一个 object 转成仅包含你需要属性的新对象,这篇文章中,我们就一起看看如何使用 投影 功能。
c# 中的投影
linq 集成查询中有两个支持投影的扩展方法,分别为: select 和 selectmany 操作,可以用它们投影单个或者多个属性,或者投影查询的结果集到一个新的匿名类型中,还可以在投影的过程中执行: 再计算,过滤,或者其他一些必要的操作。
select 投影
为了演示目的,我先构造一个 author 类,代码如下:
public class author
{
public int id { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string address { get; set; }
public author(int id, string firstname,
string lastname, string address)
{
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.address = address;
}
}
下面的代码展示了如何使用 select 操作去查询数据。
static void main(string[] args)
{
var authors = new list<author>
{
new author(1, "joydip","kanjilal", "hyderabad, india"),
new author(2, "anand","naraswamy", "cochin, india"),
new author(3, "steve","smith", "ohio, usa"),
new author(4, "uday","denduluri", "london, uk")
};
foreach (var name in authors.select(e => e.firstname))
{
console.writeline(name);
}
console.readline();
}

从上图中可以看到,所有作者的名字都展示到控制台了。
投影到 匿名类型
你可以从一个数据源中投影多个属性,也可以将查询结果投影到匿名类型中,下面的代码片段展示了如何将多个属性投影到 匿名类型 中。
static void main(string[] args)
{
var authors = new list<author>
{
new author(1, "joydip","kanjilal", "hyderabad, india"),
new author(2, "anand","naraswamy", "cochin, india"),
new author(3, "steve","smith", "ohio, usa"),
new author(4, "uday","denduluri", "london, uk")
};
var data = authors.select(e => new { e.firstname, e.lastname });
foreach (var item in data)
{
console.writeline($"{item.firstname}, {item.lastname}");
}
console.readline();
}

使用 selectmany 投影
可以使用 selectmany 从实现 ienumerable<t> 接口的集合中查询数据,还有一个,如果你想从多个集合中查询数据,可以使用 selectmany 将多个集合扁平化到一个 集合,为了演示,接下来在 author 类中新增一个 subject 属性,这个集合中包含了当前作者出版书籍的列表,如下代码所示:
public class author
{
public int id { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string address { get; set; }
public list<string> subjects { get; set; }
public author(int id, string firstname, string lastname,
string address, list<string> subjects)
{
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.address = address;
this.subjects = subjects;
}
}
接下来可以用下面的代码获取所有作者出版的书的合集。
static void main(string[] args)
{
var authors = new list<author>
{
new author(1, "joydip","kanjilal", "hyderabad, india",new list<string>{"c#", "f#"} ),
new author(2, "anand","naraswamy", "cochin, india", new list<string>{"c#", "vb.net"}),
new author(3, "steve","smith", "ohio, usa", new list<string>{"c#", "c++"}),
new author(4, "uday","denduluri", "london, uk", new list<string>{"c#", "vb.net"}),
new author(5, "jane","barlow", "london, uk", new list<string>{"c#", "c++"})
};
var data = authors.selectmany(a => a.subjects).distinct();
foreach (var subject in data)
{
console.writeline(subject);
}
console.readline();
}

使用 where 过滤结果集
可以用 where 操作符去过滤 selectmany 产生的结果集,下面的代码片段展示了满足以 j 开头的名字 并且地址包含 uk 的所有作者,并且展示这些作者的 firstname 和 subject 的合集,代码如下:
var data = authors.where(a => a.address.indexof("uk") >= 0)
.selectmany(a => a.subjects, (a, subject) => new { a.firstname, subject })
.where(n => n.firstname.startswith("j"));
foreach (var author in data)
{
console.writeline(author);
}
当执行完上面的代码后,可以看到如下的截图:

投影 在 efcore 中被大量使用,如果只想获取底层数据库中指定的列的数据,这就是 投影 要做的事,在后面的文章中,我们会讨论 投影 的其他高级功能比如:一对多关系,结果集过滤,排序等等。。。
译文链接:
到此这篇关于详解如何在c#中使用投影(projection)的文章就介绍到这了,更多相关c# 投影内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
