[英]c# get objects properties values into string array
我有一堂簡單的課:
public class Item
{
public int A {get; set;}
public int B {get; set;}
}
假設我有一個Items
集合:
var items = new List<Item>
{
new Item { A = 1, B = 2},
new Item { A = 3, B = 4}
};
我需要一個LINQ查詢,將每個Item
對象的值轉換為字符串數組,這樣,例如,我將得到:
IEnumerable<string []> result = {
{"1", "2"}, // First Item
{"3", "4"} // Second Item
}
任何想法如何完成?
items.Select(item => new [] { item.A.ToString(), item.B.ToString() }).ToList()
如果您使用的是C#7,則可以利用ValueTuple代替包含兩個元素的數組:
var result = items.Select(x => (A: x.A.ToString(), B: x.B.ToString()));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.