如何在 C# 中使用 投影(Projection)
投影(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 中被大量使用,如果只想獲取底層數據庫中指定的列的數據,這就是 投影 要做的事,在后面的文章中,我們會討論 投影 的其他高級功能比如:一對多關系,結果集過濾,排序等等。。。
譯文鏈接:https://www.infoworld.com/article/3514408/how-to-use-projections-in-c-sharp.html
總結
以上是生活随笔為你收集整理的如何在 C# 中使用 投影(Projection)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用 C# 中的 FileSystemW
- 下一篇: asp.net core中使用cooki