How to make String.Replace in LINQ?

Here is what I am trying to do, but without success. I want to call from x in list1and join y in list2 where regex.Match(x.Value).Success. After these steps I need to call String.Replaceon twice x.Value. Then call the operators onand select. I want this to look like a second example. How can this be achieved?

Here's what my listings look like:

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

Here's what my listings look like:

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

Example 1 (This is what I have):

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

Example 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;
}
+4
source share
2 answers

Are you sure what you need Regex.Match? you can get the same result without using it, anyway I added both versions ...

In version 1 °, you can use the simple If statement to check if the value has been changed

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
        };
+4

, , 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 };
+8

All Articles