【发布时间】:2014-08-27 19:24:20
【问题描述】:
这是我正在尝试做的,但没有成功。我想打电话给from x in list1 和join y in list2 where regex.Match(x.Value).Success。完成这些步骤后,我需要在x.Value 上拨打String.Replace 两次。然后调用on 和select 运算符。我希望它看起来像第二个例子。如何做到这一点?
我的列表如下所示:
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后的值?
-
你最后不应该得到一个列表吗? commonItems 列表。在您的示例中,您有 2 个列表...
-
list1 中真的可以有 NULL 值吗?这会使查询复杂化
-
@Vland 我改了,没有空值。
-
为什么 item.3 将 prod2 作为更新列表中的值?