2014-08-27 15 views
4

这是我想要做的,但没有成功。我想致电from x in list1join y in list2 where regex.Match(x.Value).Success。完成这些步骤后,我需要拨打String.Replace两次,电话号码为x.Value。然后致电onselect运营商。我希望它看起来像第二个例子。这如何成为现实?如何在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; 
} 
+0

在select中是否要返回原始值或string.replace之后的值? – Vland 2014-08-27 19:57:42

+0

最终不应该得到单个列表吗? commonItems列表。在你的例子中,你有2个列表... – Vland 2014-08-27 20:17:11

+0

你真的可以在list1中有NULL值吗?这会使查询复杂化 – Vland 2014-08-27 20:28:21

回答

4

你确定你需要Regex.Match?你可以得到相同的结果,而不使用它,反正我加了两个版本...

在1°版本,你可以使用一个简单的if语句来检查值改为

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,但以防万一这个版本将使用它。

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 
     }; 
+0

regex.match是需要的,因为我只需要对匹配的对象进行替换。 – 2014-08-27 20:45:35

+0

@BenScotch我处理在select语句中添加IF。尝试它,item.3没有取代它的值 – Vland 2014-08-27 20:47:30

+0

@ BenScotch也添加了正则表达式版本,只需检查select语句中的.Success值,以便知道是否需要更改值 – Vland 2014-08-27 20:54:37

8

如果我理解正确的,你想要的是使用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 }; 
+3

_let_我给你一个upvote那个。 – gunr2171 2014-08-27 19:29:56

+0

哪里需要在let之前,我得到这个错误:''''期望的上下文关键字'on'' – 2014-08-27 19:45:23

+0

@BenScotch检查文档关于let [here](http://msdn.microsoft.com/en-us/library /bb383976.aspx) – Dalorzo 2014-08-27 20:17:14