【问题标题】:String replace, strip one value from another in LINQ query字符串替换,在 LINQ 查询中从另一个值中删除一个值
【发布时间】:2018-08-06 13:46:11
【问题描述】:

我有以下控制器方法,其中 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

【问题讨论】:

  • 为什么是 标签?
  • 一些当前输出和期望输出的例子会很好。
  • 正则表达式是您应该查找的内容。
  • 您能否为 AppVersion 提供至少 2 个示例值?
  • 不要将查询与JsonResult 调用混用。将结果存储在变量中并修改属性。

标签: c# .net entity-framework linq


【解决方案1】:

你可以用任何东西代替它:

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

【讨论】:

  • 谢谢!正是我想要的,只是语法不太正确。
猜你喜欢
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
相关资源
最近更新 更多