LINQ 中没有任何内容可以让您创建多维数组。但是,您可以创建自己的扩展方法,该方法将返回TResult[,]
:
public static class Enumerable
{
public static TResult[,] ToRectangularArray<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult[]> selector)
{
// check if source is null
if (source == null)
throw new ArgumentNullException("source");
// load all items from source and pass it through selector delegate
var items = source.Select(x => selector(x)).ToArray();
// check if we have any items to insert into rectangular array
if (items.Length == 0)
return new TResult[0, 0];
// create rectangular array
var width = items[0].Length;
var result = new TResult[items.Length, width];
TResult[] item;
for (int i = 0; i < items.Length; i++)
{
item = items[i];
// item has different width then first element
if (item.Length != width)
throw new ArgumentException("TResult[] returned by selector has to have the same length for all source collection items.", "selector");
for (int j = 0; j < width; j++)
result[i, j] = item[j];
}
return result;
}
}
但是正如你所看到的,它仍然首先将所有结果放入交错数组TResult[][]
中,然后使用循环将其重写为多维数组。
使用示例:
string[,] array = _table.Where(x => x.IsDeleted == false)
.ToRectangularArray(x => new string[] { x.Name, x.Street });