コード例 #1
0
        /// <summary>
        /// 将字符串转变成List集合类型
        /// </summary>
        /// <typeparam name="T">类型,目前只支持int,string</typeparam>
        /// <param name="str">要转换的字符串</param>
        /// <param name="split">分隔符</param>
        /// <returns>List集合</returns>
        public static List <T> ToList <T>(string str, char split)
        {
            if (!string.IsNullOrEmpty(str))
            {
                List <T> list = new List <T>();
                foreach (string item in str.Trim(split).Split(split))
                {
                    if (string.IsNullOrEmpty(item))
                    {
                        continue;
                    }

                    list.Add(ALCommon.ConvertTo <T>(item, ALCommon.DefaultOf <T>()));
                }
                return(list);
            }
            else
            {
                return(new List <T>());
            }
        }
コード例 #2
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="TKey"></typeparam>
 /// <typeparam name="TValue"></typeparam>
 /// <param name="strKey"></param>
 /// <param name="strValue"></param>
 /// <param name="deleterepeat">自动删除重复的key</param>
 /// <returns></returns>
 public static Dictionary <TKey, TValue> ToDictionary <TKey, TValue>(string strKey, string strValue, bool deleterepeat)
 {
     if (deleterepeat)
     {
         IEnumerable <TKey>        key   = strKey.Trim(',').Split(',').Select(d => ALCommon.ConvertTo <TKey>(d, ALCommon.DefaultOf <TKey>()));
         IEnumerable <TValue>      value = strValue.Trim(',').Split(',').Select(d => ALCommon.ConvertTo <TValue>(d, ALCommon.DefaultOf <TValue>()));
         Dictionary <TKey, TValue> dict  = new Dictionary <TKey, TValue>();
         if (key != null && value != null)
         {
             for (int i = 0; i < Math.Min(key.Count(), value.Count()); i++)
             {
                 if (!dict.ContainsKey(key.ElementAt(i)))
                 {
                     dict.Add(key.ElementAt(i), value.ElementAt(i));
                 }
             }
         }
         return(dict);
     }
     else
     {
         return(ToDictionary <TKey, TValue>(strKey, strValue));
     }
 }