Convert list <int> to list <List <int>>

Is it possible to convert List<int>to List<List<int>>in C #? When I use this design

List<int>element=new List<int>();
List<List<int>>superElement= new List<List<int>>(element);

I get an error, but can I do it differently?

+5
source share
6 answers

You can do it as follows:

List<List<int>> superElement = new List<List<int>>();
superElement.Add(element);
+12
source
List<int>element=new List<int>();
List<List<int>>superElement= new List<List<int>> { element };

That will work. You cannot pass a list in the constructor.

+8
source

:

var element = new List<int>();
var superElement = new List< List<int> >(){ element };
+7

.

List<int> element = new List<int>();
List<List<int>> superElement = new List<List<int>> { element };

http://msdn.microsoft.com/en-gb/library/vstudio/bb384062.aspx

+5

List,

List<List<int>>superElement= new List<List<int>>();

; , () ,

List<List<int>>superElement= new List<List<int>>{element};

.

+4

, , , , , , , .

, , . , :

List<int> element = new List<int>();
List<List<int>> superElement = new List<List<int>>();
superElement.Add(element);

, , , , (, ) , ID . .

, , :

public class MyData
{
    public int ID {get; set;}
    public List<int> MyValues {get;set;}

    public MyData()
    {
        MyValues = new List<int>();
    }
}

:

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;

, Linq.


, :

Dictionary<int, List<int>> superElement = new Dictionary<int, List<int>>();
superElement.Add(1, element);

1 - , :

List<int> element = superElement[1];
+3

All Articles