Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,
I have two datatables dtRosterList and falsefields I want to list out all rows of dtrosterlist against falsefields ,where dtrosterlist column value is a sentence "Hello i am a list of field" and dtfalsefields column value having a single string "field"...so i am matching by Regex.IsMatch,since i can't compare directly

Regex.IsMatch("List of Facebook Link",@"\bFacebook Link\b") // true

However inside my LINQ query its returning false and i am getting empty result-set
C#
var listTobeDeleted = dtRosterList.AsEnumerable().
                     Where(r => falsefields.AsEnumerable()
                    .Select(f => f.Field<string>("FieldName"))
                    .Any(fn => Regex.Match(r.Field<string>("ListName"),
                     @"\b"+fn+"\b",RegexOptions.IgnoreCase))).CopyToDataTable();

Any help regarding REGEX would be appreciated,
Thanks!
Posted
Comments
Richard Deeming 12-Feb-15 16:12pm    
I suspect it's the same person - the Google+ link on his CodeProject article matches the name of the StackOverflow user.
PIEBALDconsult 12-Feb-15 16:30pm    
Have you considered using a DataView and LIKE rather than a Regex and all that needless copying?
Something along the lines of:
DataView listTobeDeleted = new DataView ( dtRosterList ) { RowFilter = "FieldName LIKE '%" + falsefields + "%'" } ;
It should be much quicker.
[no name] 12-Feb-15 16:52pm    
Thanks! What do you mean by falsefields here...if its a string i still have to loop over to get all the strings, if i am not wrong
DataView listTobeDeleted = new DataView ( dtRosterList ) { RowFilter = "FieldName LIKE '%" + falsefields + "%'" } ;
PIEBALDconsult 12-Feb-15 17:27pm    
Yes. Probably need to loop and form the filter with ORs.

1 solution

Two problems with your code:
  • You've used Regex.Match instead of Regex.IsMatch;
  • You've missed the @ prefix on the second "\b" string;

I'd also be inclined to add a Regex.Escape around the word to find, in case it contains any special characters.
C#
var listTobeDeleted = dtRosterList.AsEnumerable()
    .Where(r => falsefields.AsEnumerable()
    .Select(f => f.Field<string>("FieldName"))
    .Any(fn => Regex.IsMatch(r.Field<string>("ListName"), @"\b" + Regex.Escape(fn) + @"\b", RegexOptions.IgnoreCase)))
    .CopyToDataTable();
 
Share this answer
 
Comments
[no name] 12-Feb-15 16:25pm    
Thanks Richard:)
Maciej Los 12-Feb-15 16:27pm    
Good catch!
Andreas Gieriet 12-Feb-15 18:28pm    
If this is Linq2SQL, then the problem with this approach is that all data get slurped into the clients memory and the filtering is done on client side. For larger data, this can become a considerable bottleneck in terms of memory usage and especially in terms of speed. If ever possible, let the SQL server do the filtering and only get the filtered data transferred. So, Regex is only the second best approach.

Otherwise, this is the perfect solution ;-)
Have my 5!

Cheers
Andi
Richard Deeming 13-Feb-15 8:19am    
Good point. However, based on the description, and the OP's previous question[^], the objects being queried are DataTables, so this is LINQ to Objects. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900