I have a controller with following method getMail():
public JsonResult getMail(string id)
{
int Id = Convert.ToInt32(id);
string pattern = @"\bmem_id\b";
string replace = "100";
var result = (from a in db.tblmail_type
where a.Id == Id
select new MailModel
{
subject = a.Subject,
Content = Regex.Replace(a.Content, pattern, replace);
});
return Json(result, JsonRequestBehavior.AllowGet);
}
This method is used for getting mail content. Before getting content, I want to replace a "mem_id" to "100" in the mail content. Default content is given below:
Content = "You have successfully registered with Member ID: mem_id"
I have used Regex.Replace() method in the LINQ. But this code doesn't change the content. When I change this code to this form which is given below, it's work properly.
public JsonResult getMail(string id)
{
int Id = Convert.ToInt32(id);
var input = db.tblmail_type.Where(x=>x.Id==Id).FirstOrDefault().Content;
string pattern = @"\bmem_id\b";
string replace = "100";
string content = Regex.Replace(input, pattern, replace);
var result = (from a in db.tblmail_type
where a.Id == Id
select new MailModel
{
subject = a.Subject,
Content = content
});
return Json(result, JsonRequestBehavior.AllowGet);
}
Why does this happen? Can anyone specify the reason behind this weird problem? How can I replace the "Content" within the LINQ?
db.tblmail_type? I suspect the problem may be that in the first case yourRegex.Replaceis being executed in the database, which may have different regex rules.db.tblmail_type. What does VS show if you hover over it? We don't know how you're accessing the database at all.