[英]Getting a variable from a list of objects
How would you get a variable from each object in a list? 你如何从列表中的每个对象获得变量?
I've got this so far: 到目前为止我有这个:
void SortList()
{
int j = modules.Count;
string[] titles = new string[j];
for (int i = 0; i > modules.Count; i++)
{
titles[i] =
}
}
And I'm trying to get the variable "code" from each object in modules. 我正试图从模块中的每个对象获取变量“代码”。
thanks. 谢谢。
Implying modules is a list or an array, 隐含模块是列表或数组,
void SortList()
{
int j = modules.Count;
string[] titles = new string[j];
foreach (String title in modules)
{
titles[i] = title.code
}
}
As stated by Cuong Le, you could also use Linq to get a shorter version (Depending of which .Net version you are on). 正如Cuong Le所说,您也可以使用Linq来获得更短的版本(根据您所使用的.Net版本)。
titles = modules.Select(x => x.code).ToArray();
您可以使用带有Select
方法的简单代码使用LINQ:
titles = modules.Select(x => x.code).ToArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.