-1

I am trying to create a replace function, which can replace multiple strings within the 'body' fieldname of my linq queries.

This is my linq function:

  public string GetReplacedText(string body)
    {
        var data = from c in db.StoryTbls
                   where c.Body == body
                   select new
                   {
                      c.Body 
                   };

        Regex rgx2 = new Regex("<P align=right><A href>RS</A></P>");
        Regex rgx3 = new Regex("<A href>ML</A>");
        string res = rgx2.Replace(body, "");
        res = rgx3.Replace(body,"");
        return res;
    }

I am calling the above function, into the below 'httpresponse' method.

    public HttpResponseMessage getData()
    {
        if (User.IsInRole("admin"))
        {
            Regex rgx2 = new Regex("<A href></A>");

            var join = (from s in db.StoryTbls
                        join c in db.META_Cat on s.Theme equals c.metaIndex
                        where s.ACTIVE == true
                        && s.PUB_ID == 250
                        && c.Categories == "RM"
                        orderby s.ACTIVEDATE descending
                        select new
                        {
                            s.TITLE,
                            s.Body,
                            s.ACTIVEDATE,
                            c.Categories

                        }).AsEnumerable().Select(c => new NewObj
                        {
                            Title = c.TITLE,
                            Body = GetReplacedText(c.Body),
                            ActiveDate = c.ACTIVEDATE,
                            Categories = c.Categories

                        }).Take(50).ToList();

            var data = join.ToList();

            if (!data.Any())
            {
                var message = string.Format("No data found");
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
            }

However, when I call the API, the body content data is not replaced. Hence the function not working. Please advise, as to where I may be going wrong.

Many thanks

user3070072
  • 610
  • 14
  • 37
  • 2
    Parsing (or replacing) HTML with regular expressions is a [bad idea](http://stackoverflow.com/a/1732454/1081897) – D Stanley Dec 02 '14 at 15:20
  • Is the `body` string contained inside the `Body` property? – faby Dec 02 '14 at 15:20
  • @GrantWinney, I originally tried that in my function, whoever I removed the data query from my function in conjunction with jason's answer below, which has allowed it to work now. Thank you all for your suggestions & help & time. – user3070072 Dec 02 '14 at 15:28

1 Answers1

1

You need to take the output from your first call to Replace and pass it in to your second call to Replace, like so:

string res = rgx2.Replace(body, "");
res = rgx3.Replace(res, "");
Jason White
  • 218
  • 1
  • 5