这是我想要做的,但没有成功。我想致电from x in list1
和join y in list2 where regex.Match(x.Value).Success
。完成这些步骤后,我需要拨打String.Replace
两次,电话号码为x.Value
。然后致电on
和select
运营商。我希望它看起来像第二个例子。这如何成为现实?如何在LINQ中做一个String.Replace?
这里是我的名单看起来怎么样:
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;
}
在select中是否要返回原始值或string.replace之后的值? – Vland 2014-08-27 19:57:42
最终不应该得到单个列表吗? commonItems列表。在你的例子中,你有2个列表... – Vland 2014-08-27 20:17:11
你真的可以在list1中有NULL值吗?这会使查询复杂化 – Vland 2014-08-27 20:28:21