3

I have a string of data:

Key1=Value1,Key2=Value2,KeyN=ValueN

I'm trying to split the string into

List<KeyValuePair<string, string>>

I can easily do this:

List<string[]> values = item.Split( ',' ).Select( p => p.Split( '=' ) ).ToList();

but I just can't figure out the way to get that into the List of KeyValuePair's. The closest I've gotten so far is:

List<KeyValuePair<string, string>> values = item.Split( ',' )
.Select( p => new KeyValuePair<string, string>(){ p.Split( '=' ) } ).ToList();

But that's still a bit off :(

I know I can easily do it with a loop but I'd really like to get it working in Linq as practice makes perfect. I've seen quite a few examples already of similar questions like this one, but I can't seem to join the dots between those questions and mine so please forgive me if I've accidentally posted a duplicate.

Any help would really be appreciated, thanks :)

4

3 回答 3

4

到目前为止,您所做的一切都很好。然后,您有两种方法可以实现您想要的:

创建一个方法ToKeyValuePair

public static KeyValuePair<string, string> ToKeyValuePair(string[] array)
{
    if (array.Length != 2)
        throw new ArgumentException("The array must contain exactly 2 elements.");

    return new KeyValuePair<string, string>(array[0], array[1]);
}

var values = (item.Split( ',' )
                  .Select( p => ToKeyValuePair(p.Split( '=' ))))
                  .ToList();

使用 LINQ 查询语法

如果我将上面的行转换为查询语法:

var values = (from p in item.Split( ',' )
              select ToKeyValuePair(p.Split( '=' )))
             .ToList();

没有太大变化。

但是,由于有了这种新语法,很容易删除ToKeyValuePair(...)thanks tolet子句的用法:

var values = (from p in item.Split( ',' )
              let splittedP = p.Split( '=' )  // Declares a variable
              select new KeyValuePair<string, string>(splittedP[0], splittedP[1]))
             .ToList();

当然,最后一行可以用扩展方法语法(即 with .Select(p=>...))编写,但很难阅读:

var values = (item.Split(',')
                  .Select(p => new { p, splittedP = p.Split('=') })
                  .Select(p => new KeyValuePair<string, string>(p.splittedP[0], p.splittedP[1])))
                  .ToList();
于 2013-07-23T17:21:04.533 回答
1

我知道这是一个老问题,但我在谷歌上偶然发现了它。我使用公认的答案解决了这个问题,但稍微缩短了一点。你不需要new { p, splittedP = p.Split('=') }零件,只要p.Split('=')

var values = data.Split(',').Select(p=>p.Split('='))
              .Select(s => new KeyValuePair<string, string>(s[0], s[1]))
              .ToList();

如果您的密钥是唯一的,您也可以这样做:

var values = data.Split(',').Select(p => p.Split('='))
              .ToDictionary(k => k[0], v => v[1]);

这要短得多,基本上可以为您提供具有 O(1) 访问权限的列表。

(这是.NET 4.5)

于 2016-03-30T12:48:42.243 回答
0

使用上面的代码。必须修剪钥匙。

public static string GetUserInfo(this X509Certificate2 x509,X509Oid oid)
{
      try
      {
           var kvs = x509.Subject.Split(',').Select(x => new KeyValuePair<string, string>(x.Split('=')[0].Trim(), x.Split('=')[1].Trim())).ToList();

           string value = kvs.FirstOrDefault(A => A.Key == oid.ToString()).Value;
           return value;
       }
       catch (Exception)
       {   
       }
       return "";
}

添加一个枚举文件。

public enum X509Oid
{
        O,
        E,
        L,
        C,
        S,
        G,
        SN,
        CN,
        Street
}
于 2019-05-22T07:30:58.233 回答