asp.net mvc 3 - Extension method at Linq statement -


please see below:

public content getcontentbypagetitle(string pagetitle) {     return _db.contents.firstordefault(             x => hnurlhelper.urlsafe(x.pagetitle).equals(pagetitle)         ); }   public class hnurlhelper {     public static string urlsafe(string value)     {         if (!string.isnullorempty(value))         {             value = value.replace("Š", "s");              value = value.trim().tolower();              value = value.replace(" ", "-");              value = regex.replace(value, @"[^a-za-z0-9-_]", "");              return value.trim().tolower();         }         return string.empty;     } } 

server error in '/' application. linq entities not recognize method 'system.string urlsafe(system.string)' method, , method cannot translated store expression.

description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code.

exception details: system.notsupportedexception: linq entities not recognize method 'system.string urlsafe(system.string)' method, , method cannot translated store expression.

source error:

i trying work out urlsafe method within linq statement. shows error below. there know how turns out working, please ?

you may use extension method part, don't think can regex part.

the best approach that.

public static t firstordefaulturlsafe<t>(this iqueryable<t> queryable, expression<func<t, string>> propertyexpression, string pagetitle)         {             var parameter = propertyexpression.parameters[0];             var propertyname = (propertyexpression.body memberexpression).member.name;             expression body = parameter;             body = expression.property(body, propertyname);             body = expression.call(body, "replace", null, new[] { expression.constant("Š"), expression.constant("s") });             body = expression.call(body, "tolower", null);             body = expression.call(body, "trim", null);             body = expression.call(body, "replace", null, new[] { expression.constant(" "), expression.constant("-") });             body = expression.equal(body, expression.constant(pagetitle));             var lambda = expression.lambda<func<t, bool>>(body, new[] { parameter });             return queryable.firstordefault(lambda);         } 

usage in case :

return _db.contents.firstordefaulturlsafe(x => x.pagetitle, pagetitle) 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -