C# 如何将db结果转换为2D数组
我只想从表中获取两列,并将其不放在列表中,而是放在字符串的2D数组中C# 如何将db结果转换为2D数组,c#,arrays,linq,list,C#,Arrays,Linq,List,我只想从表中获取两列,并将其不放在列表中,而是放在字符串的2D数组中string[,]。以下是我的工作: string[,] array = _table.Where(x => x.IsDeleted == false) .Select(y => new string[,] {{y.Name, y.Street}}); 现在我不知道如何执行它。如果我做.ToArray()我将得到字符串[][,]。有人知道如何在不使用循环的情况下使用LINQ解决它吗?string[,]无法
string[,]
。以下是我的工作:
string[,] array = _table.Where(x => x.IsDeleted == false)
.Select(y => new string[,] {{y.Name, y.Street}});
现在我不知道如何执行它。如果我做
.ToArray()
我将得到字符串[][,]
。有人知道如何在不使用循环的情况下使用LINQ解决它吗?string[,]
无法作为LINQ查询的输出
作为替代方案,您可以尝试以下内容:-
string[][] array = _table.Where(x => x.IsDeleted == false).Select(y => new[] {y.Name, y.Streete}).ToArray();
或
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;
}
}
他想要的是
string[,]
,而不是string[]
string[,]是一个多维数组,OP提到要将其转换为2D数组。(如果我错了,请纠正我!!)他指定他想要的是string[,]
string[][]
是字符串的锯齿数组。@MarcinJuraszek:-刚刚更新了我的答案。希望这能奏效!!!!如果我完全错了,请纠正我:)@RahulTripathi仍然string[]][]
。您不能将string[,]
作为LINQ查询的输出。@我不知道为什么会被否决,但我知道==false
是问题原始查询的一部分,这就是为什么我没有将其更改为!已删除
。
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;
}
}
string[,] array = _table.Where(x => x.IsDeleted == false)
.ToRectangularArray(x => new string[] { x.Name, x.Street });