I have this following class:
public class TV
{
public string GroupId { get; set; }
public string Title { get; set; }
public string Genre { get; set; }
}
Then I have this following list (which is dynamic created and will never be same):
List<TV> myList = new List<TV>();
myList.Add(new TV { GroupId = "1", Title = "Title 1", Genre = "Genre A" });
myList.Add(new TV { GroupId = "1", Title = "Title 2", Genre = "Genre A" });
myList.Add(new TV { GroupId = "1", Title = "Title 3", Genre = "Genre A" });
myList.Add(new TV { GroupId = "3", Title = "Title 4", Genre = "Genre 18" });
myList.Add(new TV { GroupId = "A", Title = "Title 5", Genre = "Genre 18" });
myList.Add(new TV { GroupId = "A", Title = "Title 6", Genre = "Genre A" });
myList.Add(new TV { GroupId = "B", Title = "Title 7", Genre = "Genre A" });
myList.Add(new TV { GroupId = "C", Title = "Title 8", Genre = "Genre A" });
myList.Add(new TV { GroupId = "D", Title = "Title 9", Genre = "Genre 18" });
myList.Add(new TV { GroupId = "D", Title = "Title 10", Genre = "Genre A" });
myList.Add(new TV { GroupId = "D", Title = "Title 11", Genre = "Genre A" });
myList.Add(new TV { GroupId = "E", Title = "Title 12", Genre = "Genre A" });
I have the following (hardcoded) navigation categories that are links:
[0-9] [ABC] [DEF] [GHI] [JKL] [MNO] [PQR] [STU] [VWXYZ]
I’m trying to loop myList and by looking at the GroupId property, I want to place it in the appropriate div so I end up having divs like this:
<div id="group-09">
<div>
1, Title 1, Genre A
1, Title 2, Genre A
1, Title 3, Genre A
3, Title 4, Genre 18
</div>
</div>
<div id="group-ABC">
<div>
A, Title 5, Genre 18
A, Title 6, Genre A
B, Title 7, Genre A
C, Title 8, Genre A
</div>
</div>
<div id="group-DEF">
<div>
D, Title 9, Genre 18
D, Title 10, Genre A
D, Title 11, Genre A
E, Title 12, Genre A
</div>
</div>
I can’t seem to figure out the logic to properly do this!
Does anyone have a suggestion?
Thanks
You can create a dictionary that maps
GroupIds to<div>ids and group the items inmyListby<div>id: