[英]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.