常用问题总结:持续叠加

一、常用问题总结

1. 解决祖传的where 1=1

List<string> _listWhere = new List<string> { "a=b", "b=c", "b=c" };
            string str1= _listWhere.Count > 0 ? $"where { string.Join(" AND ", _listWhere)}" : string.Empty;

实例demo:

RepositoryFactory<fksd_ecoinsdraw> Repository = new RepositoryFactory<fksd_ecoinsdraw>();
            StringBuilder builder = new StringBuilder();
            string fields = "A.UserName,o.Id,o.WithdrawStyle,o.WithdrawAccount,WithdrawName,o.WithdrawMoney,o.WithdrawTime,";
            fields += "CASE WHEN IFNULL(A.UserName,'')= '' THEN A.ContactPhone else A.UserName end as WithdrawSeller,";
            fields += "o.WithdrawType,";
            fields += "CASE WHEN IFNULL(B.UserName,'')= '' THEN B.ContactPhone else B.UserName end as OpreationName,";
            fields += "o.OpreationTime,o.Remake,o.PaymentAccount ";

            builder.AppendLine($"SELECT {fields} ");
            builder.AppendLine("FROM `fksd_ecoinsdraw` AS o");
            builder.AppendLine("Left join fksdtb_user A on o.WithdrawSeller=A.Id ");
            builder.AppendLine("LEFT JOIN fksdtb_user B on o.OpreationName=B.Id ");
            List<string> whereList = new List<string>();
           bool b= !string.IsNullOrEmpty(staues);
            if (b)
            {
                if (int.Parse(staues) >= 0)
                {
                    whereList.Add($"o.WithdrawType={staues}");
                }
               
            }
            if (!string.IsNullOrEmpty(sellerAccount))
            {
                whereList.Add($"(A.UserName='{sellerAccount}'  or A.ContactPhone='{sellerAccount}') ");
            }
            if (!string.IsNullOrEmpty(ailipay))
            {
                whereList.Add($"o.WithdrawAccount='{ailipay}'");
            }
            if (!string.IsNullOrEmpty(datestart)&& !string.IsNullOrEmpty(dataEnd))
            {
                whereList.Add($"o.WithdrawTime>='{datestart}' and o.WithdrawTime<'{dataEnd}'");
            }
            else if (!string.IsNullOrEmpty(datestart) && string.IsNullOrEmpty(dataEnd))
            {
                whereList.Add($"o.WithdrawTime>='{datestart}' ");
            }
            else if (string.IsNullOrEmpty(datestart) && !string.IsNullOrEmpty(dataEnd))
            {
                whereList.Add($"  o.WithdrawTime<'{dataEnd}'");
            }
            if (whereList.Count>0)
            {
                builder.AppendLine($"where {string.Join(" AND ", whereList)}");

            }
            string total = builder.ToString().Replace(fields," count(1)");
            builder.AppendLine($"ORDER BY o.WithdrawTime DESC LIMIT {(pageIndex - 1) * pageSize},{pageSize};");
            
            List<fksd_ecoinsdraw>  List= Repository.Repository().FindListBySql(builder.ToString());
           int ob=  Convert.ToInt32( DbHelperMySQL.GetSingle(total)??0);
            fksd_ecoinsdrawAll fksd = new fksd_ecoinsdrawAll();
            fksd.data = List;
            fksd.Count = ob;

            return fksd;
string.join实例+分页实现

 

 

2. 设置input只能插入纯数字

<input type="text" oninput = "value=value.replace(/[^\d]/g,'')">

 3. 设置为6位数字的密码

  public static bool IsNumeric(string str) //接收一个string类型的参数,保存到str里
        {
            if (str == null || str.Length != 6)    //验证这个参数是否为空
                return false;                           //是,就返回False
            ASCIIEncoding ascii = new ASCIIEncoding();//new ASCIIEncoding 的实例
            byte[] bytestr = ascii.GetBytes(str);         //把string类型的参数保存到数组里

            foreach (byte c in bytestr)                   //遍历这个数组里的内容
            {
                if (c < 48 || c > 57)                          //判断是否为数字
                {
                    return false;                              //不是,就返回False
                }
            }
            return true;                                        //是,就返回True
        }

4. 获取时间戳

/// <summary>
        /// 获取时间戳 第一种
        /// </summary>
        /// <returns></returns>
        public string GetTimeStamp()
        {
            TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        } 
 /// <summary>
        /// 获取时间戳  第二种
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalMilliseconds).ToString();
        } 

 5. 通过反射拿到该程序集下所有的实例

 //获取本程序集下所有的实例
            //var o = Assembly.GetExecutingAssembly().GetTypes();
            //获取指定程序集下的实例
            var o = Assembly.Load("ConsoleApp1").GetTypes();
            foreach (var i in o)
            {
                Console.WriteLine(i.Name);
            }

 6. 操作通过遍历拿到的数据

$(obj).children().css("display", "none");

 7. 将用户名中间部分替换成*号

/// 将用户名中间部分替换成*号
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static string ReplaceUserName(string userName)
        {
            if (userName.Length >= 4)
            {
                string start = userName.Substring(0, 1);
                var index = userName.IndexOf('@');
                string end = userName.Substring(index);
                return start + "****" + end;
            }
            return userName;
        }

 8. 提取字符串中的数字

 string str = "提取123abc提取";    //我们抓取当前字符当中的123
            string result = System.Text.RegularExpressions.Regex.Replace(str, @"[^0-9]+", "");
            Console.WriteLine("使用正则表达式提取数字");
            Console.WriteLine(result);
            Console.ReadKey();

 9. 设置只可以填写英文

onkeyup="value=value.replace(/[\u4E00-\u9FA5]/g,''),getValueNum(this)"

 

posted @ 2020-05-22 10:46  锦大大的博客呀!  阅读(125)  评论(0编辑  收藏  举报