C# 转换列表<;int>;列出<;列表<;int>&燃气轮机;
我可以将C# 转换列表<;int>;列出<;列表<;int>&燃气轮机;,c#,.net,list,collections,C#,.net,List,Collections,我可以将List转换为c#中的List吗? 当我使用这种结构时 List<int>element=new List<int>(); List<List<int>>superElement= new List<List<int>>(element); liselement=newlist(); ListsuperElement=新列表(元素); 我有一个错误,但我可以用另一种方法来做吗?liselement=new Lis
List
转换为c#中的List
吗?
当我使用这种结构时
List<int>element=new List<int>();
List<List<int>>superElement= new List<List<int>>(element);
liselement=newlist();
ListsuperElement=新列表(元素);
我有一个错误,但我可以用另一种方法来做吗?liselement=new List();
List<int>element=new List<int>();
List<List<int>>superElement= new List<List<int>> { element };
ListsuperElement=新列表{element};
那就行了。无法在构造函数中传递列表。请尝试以下操作:
var element = new List<int>();
var superElement = new List< List<int> >(){ element };
var元素=新列表();
var superElement=新列表(){element};
您可以这样做:
List<List<int>> superElement = new List<List<int>>();
superElement.Add(element);
List superElement=new List();
superElement.Add(元素);
如果只想初始化列表,则
List<List<int>>superElement= new List<List<int>>();
ListsuperElement=新列表();
作品;如果您希望它最初包含一个(空)元素,那么
List<List<int>>superElement= new List<List<int>>{element};
ListsuperElement=新列表{element};
我会的。是的。您可以使用集合初始值设定项
List<int> element = new List<int>();
List<List<int>> superElement = new List<List<int>> { element };
List元素=新列表();
列表超元素=新列表{element};
由于我不相信其他答案真的花了很多精力来帮助我,我将发布我认为更完整、更有用的答案 首先,为了完整性,我将展示如何实现实际要做的事情。要创建列表列表,可以执行以下操作:
List<int> element = new List<int>();
List<List<int>> superElement = new List<List<int>>();
superElement.Add(element);
List<int> element = new List<int>();
MyData data = new MyData();
data.ID = 1;
data.MyValues = element;
List<MyData> superElement = new List<MyData>();
superElement.Add(data);
你想实现什么?我有一些数据,这些数据有ID和整数向量。我必须将这个向量保存在新的超级列表中。你能理解我吗?@MykhailoVashchuk:你应该创建一个类来保存所有你需要的信息,然后有一个列表,它是这些项目的集合。我会贴一个例子,但我觉得它不会回答问题,因为它是stands@musefan我们为什么要感到羞耻?有人问了一个问题,我们回答了。如果每个人都拒绝回答,仅仅是因为他/她觉得答案应该简单或显而易见,那么像Jon Skeet这样的用户为什么还要浪费时间向我们展示他们的知识呢?@musefan-完全不同意。OP显然很难使用集合初始值设定项,但措辞不正确,但我和其他人都清楚他想要实现什么。关于easy rep Wang,我回答这个问题得到了0个代表,最高票数是3票,因此我不知道你怎么能这么说。@MykhailoVashchuk别忘了接受你认为对你最有用的答案;)
List<int> element = new List<int>();
MyData data = new MyData();
data.ID = 1;
data.MyValues = element;
List<MyData> superElement = new List<MyData>();
superElement.Add(data);
MyData data1 = superElement.SingleOrDeafult(x => x.ID == 1);
List<int> element = data1.MyValues;
Dictionary<int, List<int>> superElement = new Dictionary<int, List<int>>();
superElement.Add(1, element);
List<int> element = superElement[1];