List 拆分集合与 读写XML配置文件

    有时候会出现需要将一个集合分成所干个集合,依次再对每组集合进行处理,想了想,用 Linq 处理就很方便

var times = Math.Ceiling((double)lis.Count() / 40);
            var temp1 = lis.Skip(0).Take(40);   //第一组
            var temp2 = lis.Skip(40).Take(40);  //第二组
            for (int i = 0; i < times; i++)
            {
                //每一组
                var ary = lis.Skip(i * 40).Take(40);
                //处理每小组数据

}
//合并集合
var list = temp1.union(temp2).ToList();

对于Web程序而言,一般情况需要的一些设备字段之类的往往是写在config 配置文件中,通过 ConfigurationManager.AppSettings["XXX"]读取,这样使用无可厚非,当然有些项目来说,配置的参数相对来说比较多,直接放在web.config中可能不易读,对于这种情况,将其全部写在xml文件中,统一管理,也比较方便,这两种方式,殊途同归。对于XML文件的读写,比较基础。

比如新建一xml 文件,将对应数据配置到XML中后,然后通过Linq to xml 进行读取操作,不怎么高大上

        //取值
        public static Dictionary<string, string> GetValue()
        {
            Dictionary<string, string> des = new Dictionary<string, string>();
            try
            {
                var obj = new object();
                lock (obj)
                {
                    var exePath = AppDomain.CurrentDomain.BaseDirectory.ToString();
                    string Path = exePath + "Config" + "\ArcConfig.xml";
                    XElement root = XElement.Load(Path);
                    var quests = from c in root.Elements() select c;
                    foreach (var item in quests)
                    {
                        des.Add(item.Name.LocalName, item.Value);
                    }
                    return des;
                }               
            }
            catch (Exception ex)
            {
                LogHelper.ErrLogQueue.Enqueue(ex.Message + "--" + ex.StackTrace);
                return des;
            }
        }

拿到字典值后,给字段对应赋值即可,这里赋值可以为成员字段,也可以放在内部缓存中

        //Cache赋值-内存
        public static void SetConfigValue(Dictionary<string, string> dic)
        {
            try
            {
                //映射成员字段//PropertyInfo[] propertyInfos =GetType().GetProperties();
                foreach (var item in dic)
                {
                    //item.SetValue(this, dic[item.Name]);  --遍历
                    CacheHelper.SetCache(item.Key, item.Value);
                }
            }
            catch (Exception ex)
            {
                LogHelper.ErrLogQueue.Enqueue(ex.Message + "---" + ex.StackTrace);
            }
        }

如此 比较轻便 灵活,使用的时候比较方便,另外配置文件放在一起,也比较便于管理

原文地址:https://www.cnblogs.com/Sientuo/p/11578902.html