List of List of different types: How to do that?

antonio

New member
Joined
Oct 15, 2017
Messages
4
Programming Experience
Beginner
Hi!
I have in my project two classes: User and Entity. I want to create a List of List of these classes. Bellow what i tried.
C#:
[COLOR=#2B91AF][FONT=Consolas]List[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]<[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]User[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]>[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] lu [/FONT][/COLOR][COLOR=#000000][FONT=Consolas]=[/FONT][/COLOR][COLOR=#0000FF][FONT=Consolas]new[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]List[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]<[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]User[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]>();[/FONT][/COLOR][COLOR=#2B91AF]List[/COLOR][COLOR=#000000]<[/COLOR][COLOR=#2B91AF]Entity[/COLOR][COLOR=#000000]>[/COLOR][COLOR=#000000] le [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#0000FF]new[/COLOR][COLOR=#2B91AF]List[/COLOR][COLOR=#000000]<[/COLOR][COLOR=#2B91AF]Entity[/COLOR][COLOR=#000000]>();[/COLOR][COLOR=#000000]

[/COLOR][COLOR=#2B91AF]User[/COLOR][COLOR=#000000] a [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#0000FF]new[/COLOR][COLOR=#2B91AF]User[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2B91AF]User[/COLOR][COLOR=#000000] b [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#0000FF]new[/COLOR][COLOR=#2B91AF]User[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2B91AF]User[/COLOR][COLOR=#000000] c [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#0000FF]new[/COLOR][COLOR=#2B91AF]User[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2B91AF]User[/COLOR][COLOR=#000000] d [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#0000FF]new[/COLOR][COLOR=#2B91AF]User[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]

[/COLOR][COLOR=#2B91AF]Entity[/COLOR][COLOR=#000000] w [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#0000FF]new[/COLOR][COLOR=#2B91AF]Entity[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2B91AF]Entity[/COLOR][COLOR=#000000] x [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#0000FF]new[/COLOR][COLOR=#2B91AF]Entity[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2B91AF]Entity[/COLOR][COLOR=#000000] y [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#0000FF]new[/COLOR][COLOR=#2B91AF]Entity[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2B91AF]Entity[/COLOR][COLOR=#000000] z [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#0000FF]new[/COLOR][COLOR=#2B91AF]Entity[/COLOR][COLOR=#000000]();[/COLOR][COLOR=#000000]

lu[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]a[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
lu[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]b[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
lu[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]c[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
lu[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]d[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]

le[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]w[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
le[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]x[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
le[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]y[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
le[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]z[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]

[/COLOR][COLOR=green]//This part dont work![/COLOR][COLOR=#000000]
[/COLOR][COLOR=#2B91AF]List[/COLOR][COLOR=#000000]<[/COLOR][COLOR=#2B91AF]List[/COLOR][COLOR=#A31515]<dynamic>[/COLOR][COLOR=#000000]>[/COLOR][COLOR=#000000] l [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#0000FF]new[/COLOR][COLOR=#2B91AF]List[/COLOR][COLOR=#000000]<[/COLOR][COLOR=#2B91AF]List[/COLOR][COLOR=#A31515]<dynamic>[/COLOR][COLOR=#000000]>();[/COLOR][COLOR=#000000]
l[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Add[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]lu[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000][FONT=Consolas]l[/FONT][/COLOR][COLOR=#000000][FONT=Consolas].[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]Add[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]([/FONT][/COLOR][COLOR=#000000][FONT=Consolas]le[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]);[/FONT][/COLOR]

To see the list i do:
C#:
[COLOR=#0000FF][FONT=Consolas]foreach[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]([/FONT][/COLOR][COLOR=#0000FF][FONT=Consolas]var[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] p [/FONT][/COLOR][COLOR=#0000FF][FONT=Consolas]in[/FONT][/COLOR][COLOR=#2B91AF][FONT=Consolas]ViewBag[/FONT][/COLOR][COLOR=#000000][FONT=Consolas].[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]usrs[/FONT][/COLOR][COLOR=#000000][FONT=Consolas])[/FONT][/COLOR]
[COLOR=#000000]{[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#A31515]<tr>[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#A31515]<td>[/COLOR][COLOR=red]@p[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]Name[/COLOR][COLOR=#000000]</[/COLOR][COLOR=#000000]td[/COLOR][COLOR=#000000]>[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#A31515]<td>[/COLOR][COLOR=red]@p[/COLOR][COLOR=#000000].[/COLOR][COLOR=#2B91AF]LasName[/COLOR][COLOR=#000000]</[/COLOR][COLOR=#000000]td[/COLOR][COLOR=#000000]>[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#000000]</[/COLOR][COLOR=#000000]tr[/COLOR][COLOR=#000000]>[/COLOR][COLOR=#000000]
[/COLOR][COLOR=#000000]}[/COLOR]
[COLOR=#2B91AF][FONT=Consolas]How[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] can i [/FONT][/COLOR][COLOR=#0000FF][FONT=Consolas]do[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] that [/FONT][/COLOR][COLOR=#0000FF][FONT=Consolas]with[/FONT][/COLOR][COLOR=#000000][FONT=Consolas] two type of objects[/FONT][/COLOR][COLOR=#000000][FONT=Consolas]?[/FONT][/COLOR]

How do i do that?
 
You can't. It's important to realise that a generic class is not really just a single class but more a template for many classes. List<User>, List<Entity> and List<dynamic> are basically three different classes. If you create a List<List<dynamic>> then each item of the outer List must be a List<dynamic>. Neither a List<User> nor a List<Entity> is a List<dynamic> so they cannot be added to a List<List<dynamic>>.

Why do you want a combined List in the first place?
 
Why do you want a combined List in the first place?
Thanks for your answer. I'm newbie in C# and ASP.NET and I would like to show in a View(MVC) a list of "users" and "entities". I know how to do that using Viewbag where I can put a object. If I want to show a list of users I could do something like:
C#:
public ActionResult Index(){
List<User> lst = List<User>();
lst.Add(usr1);
lst.Add(usr2);
lst.Add(usr3);
ViewBag.u = lst;
return View();
}

But how do I do that if I want to show a list of users and entities?
 

Latest posts

Back
Top Bottom