C# ';System.Collections.Generic.List<;字符串>';到System.Collections.Generic.List<;字符串>;[*,*]
我正在我的项目中使用C#asp.net。我用二维数组。名字叫roomno。当我试图删除其中的一行时。因此,我将数组转换为列表C# ';System.Collections.Generic.List<;字符串>';到System.Collections.Generic.List<;字符串>;[*,*],c#,arraylist,C#,Arraylist,我正在我的项目中使用C#asp.net。我用二维数组。名字叫roomno。当我试图删除其中的一行时。因此,我将数组转换为列表 static string[,] roomno = new string[100, 14]; List<string>[,] lst = new List<string>[100, 14]; lst = roomno.Cast<string>[,]().ToList(); Error 1 Invalid expressi
static string[,] roomno = new string[100, 14];
List<string>[,] lst = new List<string>[100, 14];
lst = roomno.Cast<string>[,]().ToList();
Error 1 Invalid expression term 'string' in this line...
if i try below code,
lst = roomno.Cast<string>().ToList();
静态字符串[,]roomno=新字符串[100,14];
列表[,]lst=新列表[100,14];
lst=房间编号Cast[,]().ToList();
错误1此行中的表达式术语“字符串”无效。。。
如果我尝试下面的代码,
lst=房间号Cast().ToList();
我得到
Error 3 Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<string>[*,*]'
错误3无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List[*,*]”
lst=房间号Cast().ToList()
我的代码有什么错误
之后,我计划删除列表中的行,lst.RemoveAt(array\u qty)代码>此:
List<string>[,] lst = new List<string>[100, 14];
然后它将创建一个列表
,但它仍然与列表[,]
不同
另外,roomno
只是一个二维字符串数组——就LINQ而言,它实际上是一个字符串序列——那么为什么要尝试将其转换为一个基本上是三维的类型呢
现在还不清楚你想做什么或者为什么要做,但希望这至少有助于解释为什么它不起作用
老实说,我会尽量避免在同一类型中混合使用二维数组和列表。有另一个自定义类型会有帮助吗
编辑:LINQ在二维阵列中不会有多大用处。它是为单一序列设计的。我怀疑您需要“手动”完成此操作-以下是一个简短但完整的程序示例:
using System;
class Program
{
static void Main(string[] args)
{
string[,] values = {
{"x", "y", "z"},
{"a", "b", "c"},
{"0", "1", "2"}
};
values = RemoveRow(values, 1);
for (int row = 0; row < values.GetLength(0); row++)
{
for (int column = 0; column < values.GetLength(1); column++)
{
Console.Write(values[row, column]);
}
Console.WriteLine();
}
}
private static string[,] RemoveRow(string[,] array, int row)
{
int rowCount = array.GetLength(0);
int columnCount = array.GetLength(1);
string[,] ret = new string[rowCount - 1, columnCount];
Array.Copy(array, 0, ret, 0, row * columnCount);
Array.Copy(array, (row + 1) * columnCount,
ret, row * columnCount, (rowCount - row - 1) * columnCount);
return ret;
}
}
使用系统;
班级计划
{
静态void Main(字符串[]参数)
{
字符串[,]值={
{“x”,“y”,“z”},
{“a”、“b”、“c”},
{"0", "1", "2"}
};
值=删除流(值,1);
for(int row=0;row
这是:
然后它将创建一个列表
,但它仍然与列表[,]
不同
另外,roomno
只是一个二维字符串数组——就LINQ而言,它实际上是一个字符串序列——那么为什么要尝试将其转换为一个基本上是三维的类型呢
现在还不清楚你想做什么或者为什么要做,但希望这至少有助于解释为什么它不起作用
老实说,我会尽量避免在同一类型中混合使用二维数组和列表。有另一个自定义类型会有帮助吗
编辑:LINQ在二维阵列中不会有多大用处。它是为单一序列设计的。我怀疑您需要“手动”完成此操作-以下是一个简短但完整的程序示例:
using System;
class Program
{
static void Main(string[] args)
{
string[,] values = {
{"x", "y", "z"},
{"a", "b", "c"},
{"0", "1", "2"}
};
values = RemoveRow(values, 1);
for (int row = 0; row < values.GetLength(0); row++)
{
for (int column = 0; column < values.GetLength(1); column++)
{
Console.Write(values[row, column]);
}
Console.WriteLine();
}
}
private static string[,] RemoveRow(string[,] array, int row)
{
int rowCount = array.GetLength(0);
int columnCount = array.GetLength(1);
string[,] ret = new string[rowCount - 1, columnCount];
Array.Copy(array, 0, ret, 0, row * columnCount);
Array.Copy(array, (row + 1) * columnCount,
ret, row * columnCount, (rowCount - row - 1) * columnCount);
return ret;
}
}
使用系统;
班级计划
{
静态void Main(字符串[]参数)
{
字符串[,]值={
{“x”,“y”,“z”},
{“a”、“b”、“c”},
{"0", "1", "2"}
};
值=删除流(值,1);
for(int row=0;row
如果需要使用二维列表,请尝试List()
。但在这种情况下,与数组的对话可能是:list.Select(x=>x.ToArray()).ToArray()
,这就是string[][]
,而不是string[,]
如果需要使用二维列表,请尝试list()
。但在这种情况下,与数组的对话可能是:list.Select(x=>x.ToArray()).ToArray()
,这就是string[][]
,而不是string[,]
我已经尝试过lst=roomno.Cast().ToList();还出现了错误,但无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List[,]”。我尝试删除2d数组中的一行。有什么简单的方法吗?@Sagotharan:但你到底想做什么还不清楚。请在你的问题中澄清这一点,而不是在编译之前尝试不同的语法。编码不应该是一个“试试看”的练习。@Sagotharan:你的数组没有真正的“行”-你考虑过使用字符串[][]
?还是一个列表
?再一次,如果你能提供更多真正有用的上下文,请帮助我删除2d数组中的一行。够了,我试过lst=roomno.Cast().ToList();还出现了错误,但无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List[,]”。我尝试删除2d数组中的一行。有什么简单的方法吗?@Sagotharan:但你到底想做什么还不清楚。请在你的问题中澄清这一点,而不仅仅是尝试不同的b
using System;
class Program
{
static void Main(string[] args)
{
string[,] values = {
{"x", "y", "z"},
{"a", "b", "c"},
{"0", "1", "2"}
};
values = RemoveRow(values, 1);
for (int row = 0; row < values.GetLength(0); row++)
{
for (int column = 0; column < values.GetLength(1); column++)
{
Console.Write(values[row, column]);
}
Console.WriteLine();
}
}
private static string[,] RemoveRow(string[,] array, int row)
{
int rowCount = array.GetLength(0);
int columnCount = array.GetLength(1);
string[,] ret = new string[rowCount - 1, columnCount];
Array.Copy(array, 0, ret, 0, row * columnCount);
Array.Copy(array, (row + 1) * columnCount,
ret, row * columnCount, (rowCount - row - 1) * columnCount);
return ret;
}
}