如何在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
.然后调用on
和select
运算符.我希望它看起来像第二个示例.如何做到这一点?
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?
这是我的列表的样子:
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(这是我目前拥有的):
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;
}
示例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;
}
推荐答案
您确定需要Regex.Match
吗?您可以不使用它而获得相同的结果,反正我添加了两个版本...
Are you sure you need Regex.Match
? you could obtain the same result without using it, anyway I added both versions...
在1°版本中,您可以使用简单的If语句来检查值是否已更改
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
示例代码
给了这堂课
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
,但以防万一该版本可以使用它.
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
};
这篇关于如何在LINQ中执行String.Replace?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!