c# - Merge duplicate datatable records into XML document -
i have datatable category/subcategory records in format below:
hierarchy id1 cat1 id2 cat2 id3 cat3 4 3105 mens 3195 shorts 3130 shorts 4 3105 mens 3195 shorts 3196 swim shorts 4 3105 mens 3177 knitwear 3118 jumpers 4 3105 mens 3177 knitwear 3178 cardigans 4 3105 mens 3177 knitwear 3814 v-neck knitwear
i'm trying convert xml in format this:
<category name="mens"> <categories name="shorts" /> <categories name="shorts" /> <categories name="swimshorts" /> <categories name="knitwear" /> <categories name="jumpers" /> <categories name="cardigans" /> <categories name="v-neck knitwear" />
but best can this:
<category name="mens"> <categories name="knits" /> <categories name="crew neck knitwear" /> </category> <category name="mens"> <categories name="knits" /> <categories name="cardigans" />
as can see there duplicates don't want. know need merge or de-duplicate somehow.
i'm returning data asenumerable()
, doing foreach , creating xelement top-level category , child xelements the subcategories:
var e = new xelement("category", new xattribute("id", item["did1"]), new xattribute("name", item["name1"]), item["did2"].tostring() != "" ? new xelement("categories", new xattribute("id", item["did2"]), new xattribute("name", item["name2"])) : null, item["did3"].tostring() != "" ? new xelement("categories", new xattribute("id", item["did3"]), new xattribute("name", item["name3"])) : null, item["did4"].tostring() != "" ? new xelement("categories", new xattribute("id", item["did4"]), new xattribute("name", item["name4"])) : null );
i'm not fussy technique used product results.
how this
var x = item in data group item item.cat1 g select new xelement( "category", new xattribute("name", g.key), in g group it.cat2 k select new xelement("categories2", new xattribute("name", k.key), in k.select(x=>x.cat3).distinct() select new xelement("categories3", new xattribute("name",i)) ) );
Comments
Post a Comment