詳解如何在C#中使用投影(Projection)
投影(Projection) 是一種可以將查詢結(jié)果進(jìn)行 塑性 的一種操作,你可以使用 投影 將一個(gè) object 轉(zhuǎn)成僅包含你需要屬性的新對(duì)象,這篇文章中,我們就一起看看如何使用 投影 功能。
C# 中的投影
LINQ 集成查詢中有兩個(gè)支持投影的擴(kuò)展方法,分別為: Select 和 SelectMany 操作,可以用它們投影單個(gè)或者多個(gè)屬性,或者投影查詢的結(jié)果集到一個(gè)新的匿名類(lèi)型中,還可以在投影的過(guò)程中執(zhí)行: 再計(jì)算,過(guò)濾,或者其他一些必要的操作。
Select 投影
為了演示目的,我先構(gòu)造一個(gè) Author 類(lèi),代碼如下:
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 操作去查詢數(shù)據(jù)。
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();
}

從上圖中可以看到,所有作者的名字都展示到控制臺(tái)了。
投影到 匿名類(lèi)型
你可以從一個(gè)數(shù)據(jù)源中投影多個(gè)屬性,也可以將查詢結(jié)果投影到匿名類(lèi)型中,下面的代碼片段展示了如何將多個(gè)屬性投影到 匿名類(lèi)型 中。
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 從實(shí)現(xiàn) IEnumerable<T> 接口的集合中查詢數(shù)據(jù),還有一個(gè),如果你想從多個(gè)集合中查詢數(shù)據(jù),可以使用 SelectMany 將多個(gè)集合扁平化到一個(gè) 集合,為了演示,接下來(lái)在 Author 類(lèi)中新增一個(gè) Subject 屬性,這個(gè)集合中包含了當(dāng)前作者出版書(shū)籍的列表,如下代碼所示:
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;
}
}
接下來(lái)可以用下面的代碼獲取所有作者出版的書(shū)的合集。
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 過(guò)濾結(jié)果集
可以用 Where 操作符去過(guò)濾 SelectMany 產(chǎn)生的結(jié)果集,下面的代碼片段展示了滿足以 J 開(kāi)頭的名字 并且地址包含 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);
}
當(dāng)執(zhí)行完上面的代碼后,可以看到如下的截圖:

投影 在 EFCore 中被大量使用,如果只想獲取底層數(shù)據(jù)庫(kù)中指定的列的數(shù)據(jù),這就是 投影 要做的事,在后面的文章中,我們會(huì)討論 投影 的其他高級(jí)功能比如:一對(duì)多關(guān)系,結(jié)果集過(guò)濾,排序等等。。。
譯文鏈接: https://www.infoworld.com/article/3514408/how-to-use-projections-in-c-sharp.html
到此這篇關(guān)于詳解如何在C#中使用投影(Projection)的文章就介紹到這了,更多相關(guān)C# 投影內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#采用mouse_event函數(shù)實(shí)現(xiàn)模擬鼠標(biāo)功能
這篇文章主要介紹了C#模擬鼠標(biāo)點(diǎn)擊小功能,通過(guò)代碼向大家做分析,需要的朋友可以參考下2015-07-07
詳解C# 泛型中的數(shù)據(jù)類(lèi)型判定與轉(zhuǎn)換
這篇文章主要介紹了C# 泛型中的數(shù)據(jù)類(lèi)型判定與轉(zhuǎn)換,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
C#基礎(chǔ)語(yǔ)法:結(jié)構(gòu)和類(lèi)區(qū)別詳解
這篇文章主要介紹了C#基礎(chǔ)語(yǔ)法:結(jié)構(gòu)和類(lèi)詳解,本文總結(jié)了一些結(jié)構(gòu)和類(lèi)的不同之處并給出了測(cè)試區(qū)別特性代碼,需要的朋友可以參考下2015-06-06
C#多線程之Thread中Thread.Join()函數(shù)用法分析
這篇文章主要介紹了C#多線程之Thread中Thread.Join()函數(shù)用法,實(shí)例分析了Thread.Join()方法的原理與使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
快速學(xué)習(xí)C# 設(shè)計(jì)模式之職責(zé)鏈模式
這篇文章主要介紹了C# 設(shè)計(jì)模式之職責(zé)鏈模式的的相關(guān)資料,文中代碼非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06
C#部署數(shù)據(jù)庫(kù)及IIS站點(diǎn)
這篇文章主要為大家詳細(xì)介紹了C#部署數(shù)據(jù)庫(kù)及IIS站點(diǎn)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

