[英]How can I populate and return a list of tuples in C#
I had a good suggestion on how to retun a tuple from a method: 关于如何从方法重新调整元组,我有一个很好的建议:
How can I return more than one value from a method in C# 如何从C#中的方法返回多个值
Now I realize that my code produces not just two values but an IEnumerable< >. 现在我意识到我的代码不仅产生两个值,而且产生IEnumerable <>。 Here's my code so far where result contains an IEnumerable of I guess an anonymous object containing notes and title.
到目前为止,这是我的代码,其中result包含一个IEnumerable,我猜一个包含注释和标题的匿名对象。 I am not quite sure how to put the data into the tuple and not sure how to get it out of the variable myList.
我不太确定如何将数据放入元组并且不确定如何将其从变量myList中取出。 Can I do a foreach over 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();
You could use the constructor
: 你可以使用
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);
}
But honestly what would cost you to make your code more readable and work with models: 但老实说,让你的代码更具可读性并使用模型会花费多少钱:
public class Item
{
public string Notes { get; set; }
public string Title { get; set; }
}
and then: 接着:
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,
};
}
Manipulating tuples IMHO makes the code very unreadable. 操纵元组恕我直言使代码非常难以理解。 When you start writing those
result.Item1
, result.Item2
, ..., result.Item156
things become horrible. 当你开始写那些
result.Item1
, result.Item2
,..., result.Item156
事情变得可怕。 It would be far more clear if you had result.Title
, result.Notes
, ..., wouldn't it? 如果你有
result.Title
会更清楚result.Title
result.Notes
, result.Title
。 result.Notes
,......,不是吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.