c# - Looping through datatable using regex to find exact string is slow -


i have match exact string datatable , calculate voting count each candidate. have list of votingid candidates , match votingid using regex datatble using linq. but, looping through each row of datatable find match extremely slow because have search 20 times there 20 candidates. if there 100 rows loop 2000 times.

below current code reference

list<string> votingcodes = new list<string> { "m1", "m2", "m3", "m4", "m5", "m6", "m7", "m8", "m9", "m10", "m11", "m12", "m13", "m14", "m15", "m16", "m17", "m18", "m19", "m20" };  (int = 0; <= votingcodes.count ; i++) {    foreach (datarow dr in dtfilter.rows)    {      dr["fmsg_in"] = dr["fmsg_in"].tostring().toupper();      regex r = new regex("^.*\\b" + votingcodes[i] + "\\b.*$");      dataview dv = (from t in dtfilter.asenumerable()                    r.ismatch(t.field<string>("fmsg_in") ??"") select t).asdataview();    } } 

within loop calculating votes each candidate , creating datatable bind grid.

solution - well, because of stupidity forgot remove foreach loop on dtfilter datatable not required because getting filtered data in dataview , view can sort of manipulations.

below if working code per question.

list<string> votingcodes = new list<string> { "m1", "m2", "m3", "m4", "m5", "m6", "m7", "m8", "m9", "m10", "m11", "m12", "m13", "m14", "m15", "m16", "m17", "m18", "m19", "m20" };  (int = 0; < votingcodes.count ; i++) {      regex r = new regex("^.*\\b" + votingcodes[i] + "\\b.*$");      dataview dv = (from t in dtfilter.asenumerable()                    r.ismatch(t.field<string>("fmsg_in") ??"") select t).asdataview();  } 

just move out regex object creation out foreach loop. , use regex.escape(votingcodes[i]). , i'd use var declarations. , noticed bug in for loop, fixed.

for (var = 0; < votingcodes.count; i++) {    var r = new regex("^.*\\b" + votingcodes[i] + "\\b.*$");    foreach (var dr in dtfilter.rows)    {      //...    }   //... } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -