Reputation: 2186
I have a List of tuple that i create in the following format
List<Tuple<String, String, String>> ExtendedSpecsList = new List<Tuple<String, String, String>>();
Theinputs to these tuple would be
{Audio, Type, Speakers - stereo - internal }
{Audio, Output Power / Channel, 2 Watt }
{General, Display Type, LCD monitor / TFT active matrix }
{General, Diagonal Size, 23.6" }
Based on this i am trying to create the following HTML structure for each top category/ Item1 of the tuple. Howe can i do this. Or should i use some other process other than tuple to do this...
<div class="ccs-ds-extendedSpec-group">
<div class="ccs-ds-extendedSpec-header">Audio</div>
<div class="ccs-ds-extendedSpec-body">
<table>
<tbody>
<tr>
<td class="ccs-ds-extendedSpec-item">Type</td>
<td class="ccs-ds-extendedSpec-value">Speakers - stereo - internal</td>
</tr>
<tr>
<td class="ccs-ds-extendedSpec-item" >Output Power/Channel</td>
<td class="ccs-ds-extendedSpec-value" >2 Watt</td>
</tr>
</tbody>
</table>
</div>
</div>
Upvotes: 0
Views: 588
Reputation: 203834
So the first issue is to have the structure of your data match how you want to display it. You want your data grouped on the first value, so you need to do that before binding the data:
var query = yourOriginalData.GroupBy(item => item.Item1)
.Select(group => new { Category = group.Key, Items = group });
With only that change it's ready to be bound to something that can render it.
To render it, a Repeater
would be the right choice. Here, since you have a sub-collection within your main collection you'll need nested repeaters:
<asp:Repeater runat="server" ID="repeater">
<ItemTemplate>
<div class="ccs-ds-extendedSpec-group">
<div class="ccs-ds-extendedSpec-header"><%# Eval("Category") %></div>
<div class="ccs-ds-extendedSpec-body">
<table>
<tbody>
<asp:Repeater runat="server" DataSource='<%# Eval("Items") %>'>
<ItemTemplate>
<tr>
<td class="ccs-ds-extendedSpec-item">
<%# Eval("Item2") %>
</td>
<td class="ccs-ds-extendedSpec-value">
<%# Eval("Item3") %>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
With that you only need to bind your query to this repeater and you're golden.
It's also worth noting that while Tuple
certainly works here, the readability of the code would be improved dramatically by using a class that has meaningful property names.
Upvotes: 2
Reputation: 17605
You could use Linq to filter ExtendedSpecsList
by the first component as follows.
var Audio = ExtendedSpecList.Where(iItem => iItem.First == "Audio");
var General = ExtendedSpecList.Where(iItem => iItem.First == "General");
Afterwards the different categories can be processed seperately. If the different Categories are not known beforehand, the list can be grouped as follows.
foreach(var Group in ExtendedSpecList.GroupBy(iItem => iItem.First))
{
// do some processing of the group
foreach (var iItem in Group)
{
// do something with an individual item
}
}
Upvotes: 0