I’ve the following class:
class Node
{
public string NameField{ get; set; }
public string ValueField{ get; set; }
}
And also have a list of Nodes as var test = new List<Node>, I need to make two strings arrays as string[], first contains all the NameField and the second contains all the ValueField, I did the following code:
string[] NameField = new string[test.Count];
string[] ValueField = new string[test.Count];
int i = 0;
foreach (var s in prefsNameValueArray)
{
NameField[i] = s.CTMAttrName;
ValueField[i] = s.CTMAttrValue;
i++;
}
Can I do the same using LINQ, can anybody help me to improve this code?
Thanks in advance,
Ramzy
With Linq:
Linq is not necessarily the most efficient way here since
ToArraycould create an array which may be too large(due to the doubling algorithm) if you use a query instead of a collection. But it is short and readable (and fast enough mostly).This is the for-loop version: