Home

Updating SQL database values based on REGEX matching

I'm attempting to filter results based on a matching regex, then update those results.

Something like

var list = from p in Products select p; var test = list.AsEnumerable(); Regex r = new Regex(@"^[0-9]nd"); foreach(var l in test) { if(r.IsMatch(l.FIELD_NAME)) { l.FIELD_NAME = Regex.Replace(l.FIELD_NAME,r.ToString(),"Alt"); Console.WriteLine(l.FIELD_NAME); //this works correctly, it outputs the field name replacing correctly } } test.Dump(); //this doesn't work, the values aren't changed here

It also won't work if I iterate on list and use SubmitChanges();

I know that you can't do a regex match with a SQL query, but is there anyway to do this?

Comments

  • It turns out you should refresh the table in LINQPad after you re-create it with a primary key.

    This actually works exactly as it should:
    foreach(SPEC l in list) { if(r.IsMatch(l.FIELD_NAME)) { l.FIELD_NAME = Regex.Replace(l.FIELD_NAME,r.ToString(),"Alt"); Console.WriteLine(l.FIELD_NAME); } //l.FIELD_NAME = l.FIELD_NAME + "*"; } SubmitChanges();
Sign In or Register to comment.