0

I have the following controller method where the AppVersion contains the exact value in Name, with some other string, like Name: Some Application, AppVersion: Some Application - Production. I need to strip the value of Name from the value of AppVersion. I tried to search but not sure what I am looking for.

public IActionResult GetByAppId(string Id)
{
    return new JsonResult(_db.Applications
        .Select(a => new
        {
            a.Id,
            a.AppId,
            a.Name,
            a.AppVersion
        })
        .Where(a => a.Id == Id)
        .SingleOrDefault()
    );
}

Examples of current output:

Name: Employee Recognition
AppVersion: Employee Recognition - Acceptance

Name: Content Delivery Platform (CDP)
AppVersion:  Content Delivery Platform (CDP) - pod 2 - Production
9
  • 2
    Why the <sql> tag? Commented Aug 6, 2018 at 13:47
  • 1
    Some example current output and desired output would be good. Commented Aug 6, 2018 at 13:49
  • Regular Expressions is what you should look up. Commented Aug 6, 2018 at 13:50
  • Can you please provide at least 2 example-values for AppVersion? Commented Aug 6, 2018 at 13:50
  • 1
    Don't mix the query with the JsonResult call. Store the result in a variable and modify the properties. Commented Aug 6, 2018 at 13:51

1 Answer 1

5

You can replace it with nothing:

public IActionResult GetByAppId(string Id)
{
    return new JsonResult(_db.Applications
        .Select(a => new
        {
            a.Id,
            a.AppId,
            a.Name,
            AppVersion = a.AppVersion.Replace(a.Name, "")
        })
        .Where(a => a.Id == Id)
        .SingleOrDefault()
    );
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Exactly what I was looking for, just could not quite get the syntax right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.