繁体   English   中英

如何在LINQ中执行String.Replace?

[英]How to do a String.Replace in LINQ?

这是我想要做的但没有成功。 我想from x in list1调用并join 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;
}

如果我理解你想要的是在你的查询中使用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 };

你确定需要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
        };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM