![](/img/trans.png)
[英]How to Obtain a Byte Array or Stream from a PresentationDocument object
[英]Obtain an array from an Object
我有下面的類定義。
public class people
{
public string first_name { get; set; }
public string last_name { get; set; }
public DateTime date_of_birth { get; set; }
}
然后,我創建了一系列的人,如下所示:
people[] the_people = new people[3];
the_people[0].first_name="Tony";
the_people[0].last_name="Carrot";
the_people[0].date_of_birth=new DateTime(1959-03-16);
the_people[1].first_name="Joe";
the_people[1].last_name="Tomato";
the_people[1].date_of_birth=new DateTime(1963-06-2);
the_people[2].first_name="Tarina";
the_people[2].last_name="Wends";
the_people[2].date_of_birth=new DateTime(1982-11-22);
如何將the_people對象的first_names存儲在新的字符串數組中 ,以便獲得類似以下的輸出。 是否可以通過linq
string[] the_peoples_first_names=new string[3] {"Tony","Joe","Tarina"};
同樣,我將如何獲取日期時間數組以將所有人的出生日期存儲在單獨的DateTime數組中。
您可以嘗試使用LINQ來完成。 您基本上要的是預測 。
投影是指將對象轉換為新形式的操作,該形式通常僅包含隨后將要使用的那些屬性。 通過使用投影,可以構造從每個對象構建的新類型。 您可以投影屬性並在其上執行數學函數。 您也可以投影原始對象而不更改它。
因此,我們希望將the_people
數組中的對象the_people
到新數組中。 該文檔建議使用Select
LINQ運算符:
var the_people_names = the_people.Select(p => p.first_name);
Select
內部是一個委托,通常采用lambda表達式或匿名委托的形式。
但是我們還沒有到那兒。 Select
只是一個延遲的評估,它會創建一個可枚舉的序列。 它不返回數組。 要創建一個數組,我們使用.ToArray()
:
var the_people_names_array = the_people.Select(p => p.first_name).ToArray();
您可以將這種方法用於people
任何屬性,包括出生日期。
var firstNames = the_people.Select(p => p.first_name).ToArray();
var dates_of_birth = the_people.Select(p => p.date_of_birth).ToArray();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.