public static class ListHelper
    {
        /// <summary>
        /// 将list集合 中的某一属性 组装成“,”分割的字符串(方法一)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetStr<T>(List<T> list, string str)
        {
            string result = "";
            foreach (var item in list)
            {
                string value = item.GetType().GetProperty(str).GetValue(item, null) + "";
                if (!string.IsNullOrEmpty(value))
                {
                    result = result + value + ",";
                }
            }
            return result.TrimEnd(',');
        }
        /// <summary>
        /// 将list集合 中的某一属性 组装成“,”分割的字符串(方法二)
        /// </summary>
        /// <param name="Listobj"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetStr(dynamic Listobj, string str)
        {
            string result = "";
            foreach (dynamic itemClass in Listobj)
            {
                string value = itemClass.GetType().GetProperty(str).GetValue(itemClass, null) + "";
                if (!string.IsNullOrEmpty(value))
                {
                    //itemClass.str
                    result = result + value + ",";
                }
            }
            return result.TrimEnd(',');
        }
    }

 

posted on 2019-03-05 11:08  许一朵岁月  阅读(460)  评论(0编辑  收藏  举报