IEnumerable参数数组

Asked
Viewd4267

2

使用linq?XML是否可以将IEnumerable转换为value参数的字符串数组?

 List<string> idList = new List<string>();
foreach (XElement idElement in word.Elements("id"))
{
    idList.Add(idElement.Value);
}
string[] ids = idList.ToArray();
 

这类似于

但是我需要XElement.Value参数

 IEnumerable query = ...;
MyEntityType[] array = query.Cast<MyEntityType>().ToArray();
 
4267

2 个答案

7
 string[] ids = query.Select(x => x.Value).ToArray();
 
  • Apress:Pro LINQ是学习这些XML操作中的一本好书:http://www.apress.com/book/view/9781590597897

    Jim SchubertNovember 13, 2009 18:51
  • string [] ids = word.Elements(“ id”)。Select(x => x.Value).ToArray ();

    initialZeroNovember 13, 2009 18:43
2

使用Select(x => x.Value).ToArray()