Arrays, multi-dimensional and jagged...in C# -
c# through asp.net 2.0.
in datatable have 2 columns of ids, attributeid , productattributeid.
i want loop through table , 'group' them in such way productattributeids have 1 or more attributeids associated them.
for example, in pseudo-code i'm doing
for each datarow in mydatatable.rows insert myarray @ index(productattributeid) - corresponding attributeid end foreach
so loop , each time same productattributeid present, attributeid added array corresponding.
obviously won't work arrays need size declared etc.
i've tried multi-dimensional arrays, jagged arrays, arraylists, lists of arraylists no avail, code failing yet know in theory want do.
i would, personally, use dictionary<int, list<int>>
:
foreach(var row in data) { // data... int attributeid = getattributeid(); int productattributeid = getproductattributeid(); list<int> attributes; if(!dictionary.trygetvalue(productattributeid, out attributes) { attributes = new list<int>(); dictionary[productattributeid] = attributes; } attributes.add(attributeid); }
you can of product attributes attribute:
list<int> attributeids = dictionary[productattributeid];
Comments
Post a Comment