[英]How to do a String.Replace in 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
. 我想
from x in list1
调用并join y in list2 where regex.Match(x.Value).Success
。 After these steps I need to call String.Replace
twice on x.Value
. 在这些步骤之后,我需要在
x.Value
上调用String.Replace
两次。 Then call the on
and select
operators. 然后调用
on
和select
运算符。 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): 示例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;
}
Example 2: 例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;
}
If I understood correctly what you want is to use let
in your query: 如果我理解你想要的是在你的查询中使用
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 };
Are you sure you need Regex.Match
? 你确定需要
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 在1°版本中,您可以使用简单的If语句来检查值是否已更改
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. 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.