zoukankan      html  css  js  c++  java
  • c# 开发常用小方法

    1. 判断两个列表是否为子集关系

    private bool IsSubSet(List<string> origList, List<string> curList)
    {
         return curList.All(o => origList.Any(t => t == o));
    }

    2. 比较两个list内容是否相同

     http://www.luofenming.com/show.aspx?id=ART2018042800001

    public bool CompareList(List<string> list1, List<string> list2){
        bool result = false;
        var except1 = list1.Except(list2).ToList();
        var except2 = list2.Except(list1).ToList();
        if (list1.Count == list2.Count && except1.Count == 0 && except2.Count == 0)
        {
            result = true;
        }
        return result;
    }

    3. c#反射获取所有类的属性及属性值并存入字典中

    以下方法在发送http请求参数的场景中,经常用到

    private Dictionary<string, string> GetPropertyAndValue1<T>(T dto)
    {
        var paramsDic = new Dictionary<string, string>();
        var propertyArray = dto.GetType().GetProperties();
        foreach (var property in propertyArray)
        {
            var key = property.Name.ToLower();
            if (property.GetValue(dto) != null)
            {
                paramsDic.Add(key, property.GetValue(dto).ToString());
            }
        }
    
        return paramsDic;
    }

    4 获取当前时间为本年度第几周

    /// <summary>
    /// 获取当前时间第几周
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static int GetWeekIndex(DateTime dateTime)
    {
        int firstWeekend = Convert.ToInt32(DateTime.Parse(dateTime.Year + "-1-1").DayOfWeek);
        int span = firstWeekend == 0 ? 1 : (7 - firstWeekend + 1);
        int currentDay = dateTime.DayOfYear;
        int current_week = Convert.ToInt32(Math.Ceiling((currentDay - span) / 7.0)) + 1;
        return current_week;
    }
  • 相关阅读:
    BZOJ4944 泳池 解题报告
    简短的开始
    树链剖分的一种妙用与一类树链修改单点查询问题的时间复杂度优化——2018ACM陕西邀请赛J题
    三月月考暨省队选拔
    Luogu P1245 电话号码
    JXOJ(基于UOJ)部署日志
    入学考试总结_20190310
    十二月月考之生物总结
    寒假作业完成进度
    discuz在windows下的环境配置遇到的问题总结
  • 原文地址:https://www.cnblogs.com/mo-lu/p/14270961.html
Copyright © 2011-2022 走看看