【问题标题】:How to do a String.Replace in LINQ?如何在 LINQ 中执行 String.Replace?
【发布时间】:2014-08-27 19:24:20
【问题描述】:

这是我正在尝试做的,但没有成功。我想打电话给from x in list1join y in list2 where regex.Match(x.Value).Success。完成这些步骤后,我需要在x.Value 上拨打String.Replace 两次。然后调用onselect 运算符。我希望它看起来像第二个例子。如何做到这一点?

我的列表如下所示:

list1              list2
Name    Value      Name    Value
item.1  $(prod1)   prod1   prodVal1
item.2  $(prod2)   prod2   prodVal2
item.3  prod3      prod3   prodVal3

我的列表应该是这样的:

updatedList         
Name    Value     
item.1  prodVal1   
item.2  prodVal2       
item.3  prod3      

示例 1(这是我目前拥有的):

foreach (var x in list1)
{
    Match match = reg.Match(x.Value);
    if (match.Success)
    {
        x.Value = x.Value.Replace("$(", "");
        x.Value = x.Value.Replace(")", "");
    }
}

var commonItems = from x in list1
                  join y in list2
                  on x.Value equals y.Name
                  //where regex.Match(x.Value).Success
                  select new { Item = x, NewValue = y.Value };

foreach (var x in commonItems)
{
    x.Item.Value = x.NewValue;
}

示例 2:

var commonItems = from x in list1
                  join y in list2
                  where regex.Match(x.Value).Success
                  //do x.Value.Replace("$(", "")
                  //do x.Value.Replace(")", "")
                  //then call
                  on x.Value equals y.Name
                  select new { Item = x, NewValue = y.Value };

foreach (var x in commonItems)
{
    x.Item.Value = x.NewValue;
}

【问题讨论】:

  • 在select中是要返回原值还是string.replace后的值?
  • 你最后不应该得到一个列表吗? commonItems 列表。在您的示例中,您有 2 个列表...
  • list1 中真的可以有 NULL 值吗?这会使查询复杂化
  • @Vland 我改了,没有空值。
  • 为什么 item.3 将 prod2 作为更新列表中的值?

标签: c# regex linq list


【解决方案1】:

如果我理解正确,您希望在查询中使用 let

var commonItems = from x in list1
                  join y in list2
                  let newX = x.Value.Replace("$(", "").Replace(")", "")
                  where regex.Match(x.Value).Success
                 &&  newX == y.Name
                  select new { Item = newX, NewValue = y.Value };

【讨论】:

  • 在 let 之前需要在哪里,我得到这个错误:Expected contextual keyword 'on'
  • @BenScotch 查看 let here 上的文档
【解决方案2】:

您确定需要Regex.Match 吗?不用它也能得到同样的结果,反正我加了两个版本...

在 1° 版本中,您可以使用简单的 If 语句来检查值是否已更改

NewValue = x.Value != x1 ? y.Value: x.Value 

示例代码

给定这个类

class MyClass
{
    public string Name { get; set; }
    public string Value { get; set; }
}

.

添加项目

        var list1 = new List<MyClass>();
        list1.Add(new MyClass { Name = "item.1", Value = "$(prod1)" } );
        list1.Add(new MyClass { Name = "item.2", Value = "$(prod2)" });
        list1.Add(new MyClass { Name = "item.3", Value = "prod3" });

        var list2 = new List<MyClass>();
        list2.Add(new MyClass { Name = "prod1", Value = "prodVal1" });
        list2.Add(new MyClass { Name = "prod2", Value = "prodVal2" });
        list2.Add(new MyClass { Name = "prod3", Value = "prodVal3" });

获取常用列表

        var q =  from x in list1
                 let x1 = x.Value.Replace("$(", "").Replace(")", "")
                join y in list2 on x1 equals y.Name
                select new { 
                    Item = x.Name, 
                    NewValue = x.Value != x1 ? y.Value: x.Value 
                };


        foreach (var s in q)
        {
            Console.WriteLine(s.Item + " " + s.NewValue);
        }

结果

item.1 prodVal1
item.2 prodVal2
item.3 prod3

PS:我认为你不需要Regex,但以防万一这个版本可以使用它。

var q = from x in list1
        let x1 = x.Value.Replace("$(", "").Replace(")", "")
        join y in list2 on x1 equals y.Name
        select new
        {
            Item = x.Name,
            NewValue = Regex.Match(x.Value, x1).Success ? x.Value : y.Value
        };

【讨论】:

  • regex.match 是必需的,因为我只需要对匹配的对象进行替换。
  • @BenScotch 我处理了在 select 语句中添加 IF 的问题。试试看,item.3 的值没有被替换
  • @BenScotch 还添加了 Regex 版本,只需检查 select 语句中的 .Success 值,以便您知道是否需要更改该值
  • 我需要正则表达式的原因,以便我可以检查我需要在哪些值上进行替换,否则,替换的数量超过了应有的数量。你绝对值得被接受的答案!
  • @BenScotch 我了解您的需要 Ben,这就是为什么在两个版本中我们检查 x1(替换的字符串)是否等于 x.value。所以我们知道什么时候我们应该采用新的价值,而什么时候我们应该坚持旧的价值。比简单的字符串相等检查更复杂的模式搜索通常需要Regex。不管怎样,谢谢你的投票,祝你好运:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
  • 1970-01-01
  • 2010-11-12
  • 1970-01-01
相关资源
最近更新 更多