2014-08-27 53 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