2013-04-18 60 views
2

给定字符串“a:b; c:d,e; f:g,h,i”,我想将字符串拆分为一个平面两列的列表,分别为键(在冒号左侧)和值(逗号分隔在冒号右侧)各一个。结果应该看起来像这样。如何在C#Lambda表达式中嵌套拆分的结果

{ 
    Key: "a", 
    Value: "b" 
}, 
{ 
    Key: "c", 
    Value: "d" 
}, 
{ 
    Key: "c", 
    Value: "e" 
}, 
{ 
    Key: "f", 
    Value: "g" 
}, 
{ 
    Key: "f", 
    Value: "h" 
}, 
{ 
    Key: "f", 
    Value: "i" 
} 

的问题是,我不能压扁上的所有按键逗号第二分割的结果,让我回键值的一个列表,而不是键值的名单列表。

public class KeyValue { 
    public string Key { get; set; } 
    public string Value { get; set; } 
} 

List<KeyValue> mc = "a:b;c:d,e;f:g,h,i" 
    .Split(';') 
    .Select(a => 
    { 
     int colon = a.IndexOf(':'); 
     string left = a.Substring(0, colon); 
     string right = a.Substring(colon + 1); 
     List<KeyValue> result = right.Split(',').Select(x => new KeyValue { Key = left, Value = x }).ToList(); 
     return result; 
    }) 
    .ToList(); 

感谢您的帮助。

+0

为什么你想要一个KeyValue列表的列表?您的示例只是KeyValue的列表。 –

+0

@SamLeach,我只想要一个KeyValue列表,但是请注意,该值是逗号分隔的冒号右侧的CSV中的单个值。 – Kuyenda

回答

1

你是非常接近。平整序列序列的方法是SelectMany。我们可以添加一个到你的现有代码的结束,而是因为它已经与Select结束,我们可以实际上只是将其更改为SelectMany,我们就大功告成了:

List<KeyValue> mc = "a:b;c:d,e;f:g,h,i" 
    .Split(';') 
    .SelectMany(a => 
    { 
     int colon = a.IndexOf(':'); 
     string left = a.Substring(0, colon); 
     string right = a.Substring(colon + 1); 
     List<KeyValue> result = right.Split(',') 
      .Select(x => new KeyValue { Key = left, Value = x }).ToList(); 
     return result; 
    }) 
    .ToList(); 
1
List<KeyValue> mc = "a:b;c:d,e;f:g,h,i" 
    .Split(';') 
    .Select(a => 
    { 
     int colon = a.IndexOf(':'); 
     string left = a.Substring(0, colon); 
     string right = a.Substring(colon + 1); 
     return new KeyValue() { Key = left, Value = right }; 
    }) 
    .ToList(); 

编辑:

只是为了claification的Select项目清单到一个新的类型;它已经为您处理List创建。因此,您要求输入ListList对象。所有你需要问的是对象,在这种情况下是KeyValue

+0

我认为你的解决方案缺少'Value = right'上的第三个分割,或者我错过了什么? – Kuyenda

0

您无需返回新的List<KeyValue> result。你想只返回一个KeyValue如下

string data = "a:b;c:d,e;f:g,h,i"; 

var mc = data 
    .Split(';') 
    .Select(a => 
    { 
     int colon = a.IndexOf(':'); 
     string left = a.Substring(0, colon); 
     string right = a.Substring(colon + 1); 
     return new KeyValue() { Key = left, Value = right }; 
    }).ToList(); 
+0

我认为你的解决方案缺少“Value = right”上的第三个分割,或者我错过了什么? – Kuyenda

0

与往常一样,它可以通过LINQ的执行:

string s = "a:b;c:d,e;f:g,h,i"; 
string Last = ""; 
var Result = s.Split(';', ',').Select(x => 
{ 
    if (x.Contains(":")) 
    { 
     Last = x.Split(':')[0]; 
     return new { Key = x.Split(':')[0], Value = x.Split(':')[1] }; 
    } 
    else return new { Key = Last, Value = x }; 
}); 
0
string data = "a:b;c:d,e;f:g,h,i"; 

var mc = data 
    .Split(';') 
    .SelectMany(x => { 
     int separatorIndex = x.IndexOf(':'); 
     var key = x.Substring(0, separatorIndex); 
     var values = x.Substring(separatorIndex + 1).Split(','); 

     return values.Select(v => new KeyValuePair<string,string>(key, v)); 
    }); 

Result of transform