Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a String.Replace in LINQ?

Tags:

c#

regex

list

linq

Here is what I am trying to do but with no success. I want to call from x in list1 and join y in list2 where regex.Match(x.Value).Success. After these steps I need to call String.Replace twice on x.Value. Then call the on and select operators. I want it to look something like the second example. How can this be acheived?

Here is how my lists look like:

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

Here is how my lists should look like:

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

Example 1 (This is what I currently 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;
}
like image 514
Ben Scotch Avatar asked Aug 27 '14 19:08

Ben Scotch


2 Answers

If I understood correctly what you want is to use let in your query:

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 };
like image 185
Dalorzo Avatar answered Oct 16 '22 12:10

Dalorzo


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

In the 1° version you can use a simple If statement to check if the value was changed

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

Sample code

given this class

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

.

adding items

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

getting common list

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

result

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

PS: I don't think you need Regex, but just in case this version will work using it.

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
        };
like image 24
Vland Avatar answered Oct 16 '22 12:10

Vland