Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a list with different data types of lists in it as a sublists?

I want a method to return a list which contains two more list which are having two different data types, like :

List<List<object>> parentList = new List<List<object>>();
List<string> childList1 = new List<string>();
List<DataRow> childList2 = new List<DataRow>();
parentList.Add(childList1);
parentList.Add(childList2);
return parentList;

As per above code I am getting an error

The best overloaded method match for 'System.Collections.Generic.List>.Add(System.Collections.Generic.List)' has some invalid arguments

Please can anyone suggest me the best approach to handle this.

Thanks

like image 574
nik_boyz Avatar asked Dec 30 '15 04:12

nik_boyz


People also ask

Can a list contains different data types?

Lists are the most versatile of Python's compound data types. A list contains items separated by commas and enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is that all the items belonging to a list can be of different data type.

Can a list be nested in another list?

A nested list is a list of lists, or any list that has another list as an element (a sublist). They can be helpful if you want to create a matrix or need to store a sublist along with other data types.


1 Answers

What about creating an object of your class as like this?

 public class myParent
    {
        public List<string> childList1 = new List<string>();
        public List<DataRow> childList2 = new List<DataRow>();
    }
 public void someFun()
  {
        List<myParent> parentList = new List<myParent>();
        myParent myParentObject = new myParent();
        myParentObject.childList1 = new List<string>() { "sample" };
        myParentObject.childList2 = new List<DataRow>() { };
        parentList.Add(myParentObject);
   }
like image 187
sujith karivelil Avatar answered Oct 19 '22 23:10

sujith karivelil