I’m trying to refactor some slow running code which writes XML using nested loops of several datatables. I read that using linq to write the xml would be faster. I’m not well versed in linq, so I was hoping to get some help here.
Some things I need to mention is that the current architecture uses a webservice which returns data to us in dataTables. We then go through the datatables (iteratively), and there are several which results several nested loops.
example:
dt1 = Webservice.getStuff();
for each (datarow r1 in dt1.Rows) {
dt2 = Webservice.getMoreStuff(r1[col1], r1[col2]);
// write out some xml
for each (datarow r2 in dt2.Rows) {
dt3 = Webservice.getEvenMoreStuff(r2[col1], r2[col2]);
// write out more xml
for each (datarow r3 in dt3.Rows) {
// write out more xml
}
}
}
As you can see for obvious reasons, this is terribly slow. Is there a way to speed this up using linq? What would you guys suggest as a more efficient approach to refactor this? I’m sorry if the details are vague…
I appreciate any help anyone could offer.
Writing the XML isn’t what is slowing you down in this case. That speed should be negligible compared to the time spent making all of the web service calls.
Instead of focusing on the writing of the XML, I would try to figure out how to compress the number of web service calls so you can get all your data at once rather than making nested calls like that.