Using LINQ to SQL and chained Replace

Posted by White Dragon on Stack Overflow See other posts from Stack Overflow or by White Dragon
Published on 2009-04-30T15:20:42Z Indexed on 2010/04/17 18:03 UTC
Read the original article Hit count: 385

Filed under:
|
|

I have a need to replace multiple strings with others in a query

from p in dx.Table
where p.Field.Replace("A", "a").Replace("B", "b").ToLower() = SomeVar
select p

Which provides a nice single SQL statement with the relevant REPLACE() sql commands.

All good :)

I need to do this in a few queries around the application... So i'm looking for some help in this regard; that will work as above as a single SQL hit/command on the server

It seems from looking around i can't use RegEx as there is no SQL eq

Being a LINQ newbie is there a nice way for me to do this?

eg is it possible to get it as a IQueryable "var result" say and pass that to a function to add needed .Replace()'s and pass back? Can i get a quick example of how if so?

EDIT: This seems to work! does it look like it would be a problem?

var data = from p in dx.Videos select p;
data = AddReplacements(data, checkMediaItem);
theitem = data.FirstOrDefault();

...

public IQueryable<Video> AddReplacements(IQueryable<Video> DataSet, string checkMediaItem)
{
    return DataSet.Where(p =>
            p.Title.Replace(" ", "-").Replace("&", "-").Replace("?", "-") == checkMediaItem);
}

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about linq-to-sql