LINQ到一个字符串转换为词典<字符串,字符串> [英] Linq to convert a string to a Dictionary<string,string>

查看:147
本文介绍了LINQ到一个字符串转换为词典<字符串,字符串>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的字典举办一些参数和我刚刚发现这是不可能的序列化任何实现的IDictionary(的无法序列化的IDictionary )。



作为一种变通方法,我想可能会转换成字典序列化字符串然后在需要时转换回一本字典。



由于我想提高我的LINQ这似乎是一个好地方做,但我不知道如何下手。



这是我怎么想实现它没有LINQ:

  ///<总结> 
///获取/设置FTPS处理器
///<的扩展属性; /总结>
///<&言论GT;不能序列化Dictionary对象,以便转换成字符串(http://msdn.microsoft.com/en-us/library/ms950721.aspx)</remarks>
公众解释<字符串,字符串> FtpsExtendedProperties
{
得到
{
&字典LT;字符串,字符串>字典=新词典<字符串,字符串>();

//从字符串
的String [] = kvpArray的m_FtpsExtendedProperties.Split键值对('|');

的foreach(在kvpArray串KVP)
{
//单独的键和值来构建字典
的String [] =对kvp.Split(', ');
dict.Add(对[0],对[1]);
}

返回字典;
}


{
串newProperties =的String.Empty;

//迭代通过字典转换值对转换成字符串
的foreach(KeyValuePair<字符串,字符串> KVP价值)
{
newProperties + =字符串.Format({0},{1} |,kvp.Key,kvp.Value);
}

//删除最后一个管道serperator
newProperties = newProperties.Substring(0,newProperties.Length - 1);
}
}


解决方案

在你的代码的情况下

  ///获取/设置FTPS处理器
///<的扩展属性; /总结>
///<&言论GT;不能序列化Dictionary对象,以便转换成字符串(http://msdn.microsoft.com/en-us/library/ms950721.aspx)</remarks>
公众解释<字符串,字符串> FtpsExtendedProperties
{
得到
{

&字典LT;字符串,字符串>字典= m_FtpsExtendedProperties.Split('|')
。选择(S = GT; s.Split(''))
.ToDictionary(键=>键[0] .Trim()值=>值[1] .Trim());

返回字典;
}


{

//注意:对于大词典,这可以用
//一个StringBuilder而不是字符串为cumulativeText

//不保留字典顺序(如果这是很重要的 - 重新排序的String.Format)
串newProperties =
value.Aggregate(,
(cumulativeText,KVP)= GT;的String.Format({0},{1} | {2},kvp.Key,kvp.Value,cumulativeText));

//删除最后一个管道serperator
newProperties = newProperties.Substring(0,newProperties.Length - 1);

}
}



没有测试过这一点,但使用的功能应该给你如何使用LINQ


做到这一点相当简洁一些想法

I'm using a dictionary to hold some parameters and I've just found out that it's not possible to serialize anything that implements IDictionary (unable to serialize IDictionary).

As a workaround I'd like to convert may dictionary into a string for serialization and then convert back to a dictionary when required.

As I'm trying to improve my LINQ this seems like a good place to do it but I'm not sure how to start.

This is how I'd implement it without LINQ:

/// <summary>
/// Get / Set the extended properties of the FTPS processor
/// </summary>
/// <remarks>Can't serialize the Dictionary object so convert to a string (http://msdn.microsoft.com/en-us/library/ms950721.aspx)</remarks>
public Dictionary<string, string> FtpsExtendedProperties
{
    get 
    {
        Dictionary<string, string> dict = new Dictionary<string, string>();

        // Get the Key value pairs from the string
        string[] kvpArray = m_FtpsExtendedProperties.Split('|');

        foreach (string kvp in kvpArray)
        {
            // Seperate the key and value to build the dictionary
            string[] pair = kvp.Split(',');
            dict.Add(pair[0], pair[1]);
        }

        return dict; 
    }

    set 
    {
        string newProperties = string.Empty;

        // Iterate through the dictionary converting the value pairs into a string
        foreach (KeyValuePair<string,string> kvp in value)
        {
            newProperties += string.Format("{0},{1}|", kvp.Key, kvp.Value);    
        }

        // Remove the last pipe serperator
        newProperties = newProperties.Substring(0, newProperties.Length - 1);
    }
}

解决方案

In the context of your code

/// Get / Set the extended properties of the FTPS processor
/// </summary>
/// <remarks>Can't serialize the Dictionary object so convert to a string (http://msdn.microsoft.com/en-us/library/ms950721.aspx)</remarks>
public Dictionary<string, string> FtpsExtendedProperties
{
get 
{

Dictionary<string, string> dict = m_FtpsExtendedProperties.Split('|')
      .Select(s => s.Split(','))
      .ToDictionary(key => key[0].Trim(), value => value[1].Trim());

    return dict; 
}

set 
{

        // NOTE: for large dictionaries, this can use
        // a StringBuilder instead of a string for cumulativeText

        // does not preserve Dictionary order (if that is important - reorder the String.Format)
    string newProperties = 
              value.Aggregate ("",
                      (cumulativeText,kvp) => String.Format("{0},{1}|{2}", kvp.Key, kvp.Value, cumulativeText));

        // Remove the last pipe serperator
        newProperties = newProperties.Substring(0, newProperties.Length - 1);

        }
    }

Haven't tested this, but the functions used should give you some idea of how to do it fairly succinctly with LINQ

这篇关于LINQ到一个字符串转换为词典&LT;字符串,字符串&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆