[英]How can I populate and return a list of tuples in C#
關於如何從方法重新調整元組,我有一個很好的建議:
現在我意識到我的代碼不僅產生兩個值,而且產生IEnumerable <>。 到目前為止,這是我的代碼,其中result包含一個IEnumerable,我猜一個包含注釋和標題的匿名對象。 我不太確定如何將數據放入元組並且不確定如何將其從變量myList中取出。 我可以對myList進行預測嗎?
public static IEnumerable< Tuple<string, string> > GetType6()
{
var result =
from entry in feed.Descendants(a + "entry")
let notes = properties.Element(d + "Notes")
let title = properties.Element(d + "Title")
// Here I am not sure how to get the information into the Tuple
//
}
var myList = GetType6();
你可以使用constructor
:
public static IEnumerable<Tuple<string, string>> GetType6()
{
return
from entry in feed.Descendants(a + "entry")
let notes = properties.Element(d + "Notes")
let title = properties.Element(d + "Title")
select new Tuple<string, string>(notes.Value, title.Value);
}
但老實說,讓你的代碼更具可讀性並使用模型會花費多少錢:
public class Item
{
public string Notes { get; set; }
public string Title { get; set; }
}
接着:
public static IEnumerable<Item> GetType6()
{
return
from entry in feed.Descendants(a + "entry")
let notes = properties.Element(d + "Notes")
let title = properties.Element(d + "Title")
select new Item
{
Notes = notes.Value,
Title = title.Value,
};
}
操縱元組恕我直言使代碼非常難以理解。 當你開始寫那些result.Item1
, result.Item2
,..., result.Item156
事情變得可怕。 如果你有result.Title
會更清楚result.Title
result.Notes
, result.Title
。 result.Notes
,......,不是嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.