|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Filtering Dataviewsin the middle of strings. So, for example: col LIKE 'A*B' doesn't work, i.e. it doesn't return all rows where col starts with A and ends with B and has anything inbetween. To get around this I derived my own class from DataView and parsed the RowFilter string like this: // Look for wildcards that DO NOT occur at the start or end of the string Regex reg = new Regex(@"(?<=.)\*(?=.)"); string strReplace = "*' AND " + col + " LIKE '*"; RowFilter = reg.Replace(strWild,strReplace); so col LIKE 'A*B' becomes col LIKE 'A*' AND col LIKE '*B' this works quite well except in this case: col LIKE '*A*B*' which becomes: col LIKE '*A*' AND col LIKE '*B*' this will work, it'll give you rows that contain and A followed by a B anywhere in the string, but it also returns strings that contain B followed by A. For example, given these rows: xxxAxxxBxxx xxABxxxxxxx xxxBxxxAxxx xxxAxxxxxxx The first 3 rows would be returned, but row 3 should not have been returned since A and B are in the wrong order. Does anybody have any ideas on how I could handle this? Or is there an easier way to deal with wildcards? And why doesn't DataView support wildcards in the middle of strings in the first place? Thanks You can try
col LIKE 'A%B' HTH Elton Wang elton_w***@hotmail.com >-----Original Message----- allow for wildcards >The DataView RowFilter property rather annoyingly doesn't >in the middle of strings. So, for example: starts with A and > >col LIKE 'A*B' > >doesn't work, i.e. it doesn't return all rows where col >ends with B and has anything inbetween. To get around this I derived my own >class from DataView and parsed the RowFilter string like end of the stringthis: > >// Look for wildcards that DO NOT occur at the start or Show quote >Regex reg = new Regex(@"(?<=.)\*(?=.)"); followed by a B >string strReplace = "*' AND " + col + " LIKE '*"; >RowFilter = reg.Replace(strWild,strReplace); > >so > >col LIKE 'A*B' > >becomes > >col LIKE 'A*' AND col LIKE '*B' > >this works quite well except in this case: > >col LIKE '*A*B*' > >which becomes: > >col LIKE '*A*' AND col LIKE '*B*' > >this will work, it'll give you rows that contain and A >anywhere in the string, but it also returns strings that contain B followed >by A. For example, given these rows: have been returned > >xxxAxxxBxxx >xxABxxxxxxx >xxxBxxxAxxx >xxxAxxxxxxx > >The first 3 rows would be returned, but row 3 should not >since A and B are in the wrong order. Does anybody have any ideas on how I >could handle this? Or is there an easier way to deal with wildcards? And why >doesn't DataView support wildcards in the middle of strings in the first Show quote >place? > >Thanks >. > Unfortunately that doesn't work. The RowFilter property of the dataview does
not allow wildcards in the middle of a string, only at the begining or the end. This is the problem I am trying to work around. Show quote "Elton Wang" wrote: > You can try > > col LIKE 'A%B' > > HTH > > Elton Wang > elton_w***@hotmail.com > > >-----Original Message----- > >The DataView RowFilter property rather annoyingly doesn't > allow for wildcards > >in the middle of strings. So, for example: > > > >col LIKE 'A*B' > > > >doesn't work, i.e. it doesn't return all rows where col > starts with A and > >ends with B and has anything inbetween. To get around > this I derived my own > >class from DataView and parsed the RowFilter string like > this: > > > >// Look for wildcards that DO NOT occur at the start or > end of the string > >Regex reg = new Regex(@"(?<=.)\*(?=.)"); > >string strReplace = "*' AND " + col + " LIKE '*"; > >RowFilter = reg.Replace(strWild,strReplace); > > > >so > > > >col LIKE 'A*B' > > > >becomes > > > >col LIKE 'A*' AND col LIKE '*B' > > > >this works quite well except in this case: > > > >col LIKE '*A*B*' > > > >which becomes: > > > >col LIKE '*A*' AND col LIKE '*B*' > > > >this will work, it'll give you rows that contain and A > followed by a B > >anywhere in the string, but it also returns strings that > contain B followed > >by A. For example, given these rows: > > > >xxxAxxxBxxx > >xxABxxxxxxx > >xxxBxxxAxxx > >xxxAxxxxxxx > > > >The first 3 rows would be returned, but row 3 should not > have been returned > >since A and B are in the wrong order. Does anybody have > any ideas on how I > >could handle this? Or is there an easier way to deal with > wildcards? And why > >doesn't DataView support wildcards in the middle of > strings in the first > >place? > > > >Thanks > >. > > > Hi,
Try using a dataview.select instead. That syntax should allow you to use like as you wish. HTH, Bernie Yaeger Show quote "wjousts" <wjou***@discussions.microsoft.com> wrote in message news:AC9236A8-CB26-40C7-9DF2-089F4B2D4696@microsoft.com... > The DataView RowFilter property rather annoyingly doesn't allow for > wildcards > in the middle of strings. So, for example: > > col LIKE 'A*B' > > doesn't work, i.e. it doesn't return all rows where col starts with A and > ends with B and has anything inbetween. To get around this I derived my > own > class from DataView and parsed the RowFilter string like this: > > // Look for wildcards that DO NOT occur at the start or end of the string > Regex reg = new Regex(@"(?<=.)\*(?=.)"); > string strReplace = "*' AND " + col + " LIKE '*"; > RowFilter = reg.Replace(strWild,strReplace); > > so > > col LIKE 'A*B' > > becomes > > col LIKE 'A*' AND col LIKE '*B' > > this works quite well except in this case: > > col LIKE '*A*B*' > > which becomes: > > col LIKE '*A*' AND col LIKE '*B*' > > this will work, it'll give you rows that contain and A followed by a B > anywhere in the string, but it also returns strings that contain B > followed > by A. For example, given these rows: > > xxxAxxxBxxx > xxABxxxxxxx > xxxBxxxAxxx > xxxAxxxxxxx > > The first 3 rows would be returned, but row 3 should not have been > returned > since A and B are in the wrong order. Does anybody have any ideas on how I > could handle this? Or is there an easier way to deal with wildcards? And > why > doesn't DataView support wildcards in the middle of strings in the first > place? > > Thanks Thanks for the reply.
I assume you mean DataTable.Select? Unfortunately that using the same rules for the filter expression as DataView.RowFilter, i.e. the rules for DataColumn's Expression property, and suffers with the same limitation, no wild cards in the middle of strings. Show quote "Bernie Yaeger" wrote: > Hi, > > Try using a dataview.select instead. That syntax should allow you to use > like as you wish. > > HTH, > > Bernie Yaeger > > "wjousts" <wjou***@discussions.microsoft.com> wrote in message > news:AC9236A8-CB26-40C7-9DF2-089F4B2D4696@microsoft.com... > > The DataView RowFilter property rather annoyingly doesn't allow for > > wildcards > > in the middle of strings. So, for example: > > > > col LIKE 'A*B' > > > > doesn't work, i.e. it doesn't return all rows where col starts with A and > > ends with B and has anything inbetween. To get around this I derived my > > own > > class from DataView and parsed the RowFilter string like this: > > > > // Look for wildcards that DO NOT occur at the start or end of the string > > Regex reg = new Regex(@"(?<=.)\*(?=.)"); > > string strReplace = "*' AND " + col + " LIKE '*"; > > RowFilter = reg.Replace(strWild,strReplace); > > > > so > > > > col LIKE 'A*B' > > > > becomes > > > > col LIKE 'A*' AND col LIKE '*B' > > > > this works quite well except in this case: > > > > col LIKE '*A*B*' > > > > which becomes: > > > > col LIKE '*A*' AND col LIKE '*B*' > > > > this will work, it'll give you rows that contain and A followed by a B > > anywhere in the string, but it also returns strings that contain B > > followed > > by A. For example, given these rows: > > > > xxxAxxxBxxx > > xxABxxxxxxx > > xxxBxxxAxxx > > xxxAxxxxxxx > > > > The first 3 rows would be returned, but row 3 should not have been > > returned > > since A and B are in the wrong order. Does anybody have any ideas on how I > > could handle this? Or is there an easier way to deal with wildcards? And > > why > > doesn't DataView support wildcards in the middle of strings in the first > > place? > > > > Thanks > > > |
|||||||||||||||||||||||