字符串替换,在LINQ查询中从另一个值中剥离一个值



我有以下控制器方法,其中AppVersion包含Name中的确切值,以及其他一些字符串,如Name:some Application、AppVersion:some Application-Production。我需要从AppVersion的值中去掉Name的值。我试着搜索,但不确定我在找什么。

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()
);
}

电流输出示例:

Name: Employee Recognition
AppVersion: Employee Recognition - Acceptance
Name: Content Delivery Platform (CDP)
AppVersion:  Content Delivery Platform (CDP) - pod 2 - Production

您可以不使用任何东西来替换它:

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()
);
}

最新更新