Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following question.
Is it possible to ad an list into a list?

Wrapper class
C#
public List<Analyses> HaalAnalyseOp(DataSource requestedDataSource)
        {
            IDataSource source;

            switch (requestedDataSource)
            {
                case DataSource.Access:
                    source = new AccessDataSource();
                    break;
                case DataSource.MsSql:
                    source = new MsSqlDataSource();
                    break;
                case DataSource.WebService:
                    source = new WebServiceDataSource();
                    break;
                default:
                    throw new ArgumentOutOfRangeException("requestedDataSource");
            }

            return source.GetAnalyses();
        }

        public class MsSqlDataSource : IDataSource
        {
            public List<Analyses> GetAnalyses()
            {
                //code to get MsSql data
                throw new NotImplementedException();
            }
        }

        public class AccessDataSource : IDataSource
        {
            public List<Analyses> GetAnalyses()
            {
                var lijst = new List<Analyses>();
                lijst.Add(new Analyses() { Stappen = "OdbcDataAdapter da = new OdbcDataAdapter();" });                                      // list 1
                return lijst;
            }
        }

        public class WebServiceDataSource : IDataSource
        {
            public List<Analyses> GetAnalyses()
            {
                //code to get WebService data
                throw new NotImplementedException();
            }
        }
    }


IDataSource class
C#
public class Analyses
    {
        public List<string> Stappen { get; set; }          //List 2
    }

    public interface IDataSource
    {
        List<Analyses> GetAnalyses();
    }
Posted
Comments
CPallini 14-Jan-14 5:18am    
Of course it is possible. What is the problem (if any) with your code?
Member 10380897 14-Jan-14 5:41am    
I get an error because the stappen field is an List(list 2). I can't put that list into the lijst list. (list 1)

Cannot implicitly convert type 'string' to 'System.Collections.Generic.List'
Maarten Kools 14-Jan-14 5:49am    
lijst.Add(new Analyses() { Stappen = "OdbcDataAdapter da = new OdbcDataAdapter();" });
Stappen is a list, not a string. So instead of Stappen = "OdbcDataAdapter da = new OdbcDataAdapter();" do Stappen = new List<string> { "OdbcDataAdapter da = new OdbcDataAdapter();"}

[edit]sometimes I get so fed up with CP's tendency to change brackets! Hope you're not getting spammed with email because of this[/edit]
Member 10380897 14-Jan-14 6:19am    
Ok thx it works. Now i have a sub question related to the previous one.

This is my class structure:

Dal
| |
| |__ Webserver (Database type 1)
| |__ Own Database (Database type 2)
|
|
|__ Wrapper (Converts the data for database one or two)

The Wrapper returns the following list to the DAL for my Access database. (String format)

<pre lang="c#">
public class AccessDataSource : IDataSource
{
public List<analyses> GetAnalyses()
{
var lijst = new List<analyses>();
lijst.Add(new Analyses()
{
Stappen = new List<string> { "OdbcDataAdapter da = new OdbcDataAdapter();",
"DataSet ds = new DataSet();",
"DataTable dt = new DataTable();",
"da.SelectCommand = new OdbcCommand(@\"SELECT * FROM Recepten WHERE id = \" + Id + \"\", connectie);",
"da.Fill(ds, \"AnalysesID\");",
"dt = ds.Tables[\"AnalysesID\"]"
}
});
return lijst;
}
}
</pre>


How can i execute the list in my dal to select data from my access file?
Suk@nta 14-Jan-14 5:51am    
yes u cannot convert a string to System.Collections.Generic.List
u have to assign a list type.

 
Share this answer
 
C#
List<list><int>> list = new List<list><int>>();
    var rand = new Random();
    for (int i = 0; i < 10; i++)
    {
        List<int> sublist = new List<int>();
        int top = rand.Next(1, 15);
        for (int v = 0; v < top; v++)
        {
        sublist.Add(rand.Next(1, 5));
        }
        list.Add(sublist);


this is the small example to how we can list inside list
let me know if its help or not
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900