I have a view model
public class PersonsViewmodel
{
public string FirstName { get; set; }// from DB
public string LastName { get; set; }// from DB
public string FullName { get; set; } // this should be a custom value
}
The linq query
var per = from p in db.Persons where p.Active==0
select new PersonsViewmodel
{ FirstName =p.FirstName ,LastName =p.LastName ,
FullName =Reg(p.FirstName)
};
public static string Reg(string str)
{
return str = Regex.Replace(str, "[^a-zA-Z0-9]+", "-", RegexOptions.Compiled);
}
This throws a error
LINQ to Entities does not recognize the method ‘System.String
reg(System.String)’ method, and this method cannot be translated into
a store expression.
Is there a better way to call this Reg function in the model itself instead of calling in the linq query or should i call the function after the linq query?
Right now I am doing this operation in the View like
@{
var FullName = Regex.Replace(model.FirstName, "[^a-zA-Z0-9]+", "-");
}
and using the variable FullName to render.
Isn’t that a job for the view model?