List GroupBy真实用法,Reflection(反射)用法,Enum用法,正则,搜索下拉布局

 1.List  GroupBy 用法 

 var _roomProducts = homesingProducts.GroupBy(t => t.RoomName);
        RoomedProducts temp = new RoomedProducts();
        foreach (var item in _roomProducts)
        {
            roomNames.Add(item.Key);

            temp = new RoomedProducts();
            temp.RoomName = item.Key;
            temp.Products = new List<TempProductInfo>();
            temp.Products.AddRange(item);
           
            roomedProducts.Add(temp);
        }

2.枚举类配合反射使用(避免到处出现  order.state == 50   order.statedesc =="已付款"  等等带有常量的代码)

public enum DesignOrderState
{

    /// <summary>
    /// 待付款
    /// </summary>
    [Description("待付款")]
    WaitPaying = 30,


    /// <summary>
    /// 已付款
    /// </summary>
    [Description("已付款")]
    Paid = 50,


    /// <summary>
    /// 设计中
    /// </summary>
    [Description("设计中")]
    Designing = 70,


    /// <summary>
    /// 已交付
    /// </summary>
    [Description("已交付")]
    Completed = 90,

}


/// <summary>
    /// 根据类名获取所有public属性名和特性Description
    /// </summary>
    /// <param name="className">类名</param>
    /// <returns>Dictionary<属性名,Description></returns>
    public static Dictionary<string, string> ReflectFiledsNameByClassName(string className)
    {
        Dictionary<string, string> result = new Dictionary<string, string>();
        Type t = Type.GetType(className);
        PropertyInfo[] properties = t.GetProperties();
        string attr = "";
        foreach (var item in properties)
        {
            foreach (Attribute _attr in Attribute.GetCustomAttributes(item))
            {
                if (_attr.GetType() == typeof(DescriptionAttribute))
                {
                    attr = ((DescriptionAttribute)_attr).Description;
                }
            }
            result.Add(item.Name, attr);
            attr = "";
        }
        return result;
    }


    /// <summary>
    /// 获取枚举类的字段名及值
    /// </summary>
    /// <param name="type"> typeof(枚举类) </param>
    /// <returns></returns>
    public static Dictionary<string, int> ReflectEnumFiledAndValues(Type type)
    {
        Dictionary<string, int> result = new Dictionary<string, int>();

        FieldInfo[] properties = type.GetFields(); 
        foreach (var item in properties)
        {
            try
            {
                result.Add(item.Name, (int)(Enum.Parse(type, item.Name)));// 得到的第一个属性是 value_  暂时还不知道为什么
            }
            catch (Exception)
            {
               
            }
        }
        return result;

    }


    /// <summary>
    /// 根据枚举类的字段名获取特性Description中的中文描述信息
    /// </summary>
    /// <param name="className">枚举类名  示:SoftOrderState</param>
    /// <param name="filedName">枚举类字段名    示:(SoftOrderState)30  </param>
    /// <returns></returns>
    public static string ReflectFiledsNameByEnumClassName(string className, string filedName)
    {
        string result = "";
        Type t = Type.GetType(className);
        FieldInfo[] properties = t.GetFields();
        int length = properties.Length;
        for (int i = 1; i < length; i++)
        {
            if (properties[i].Name == filedName)
            {
                DescriptionAttribute attr = Attribute.GetCustomAttribute(properties[i],
             typeof(DescriptionAttribute), false) as DescriptionAttribute;
                result = attr.Description;
                break;
            }
        }

        return result;
    }


 Dictionary<string, string> fileds = new Dictionary<string, string>();
        Dictionary<string, int> filedValues = new Dictionary<string, int>();


ReflectFiledsNameByEnumClassName(className, SoftOrderState.Sending.ToString()));   //属性 找 Description 

ReflectFiledsNameByEnumClassName(className,((SoftOrderState)30).ToString())); // 值 找 Description

(int)((Enum.Parse(typeof(SoftOrderState), "Sending"))));     // 属性 to 值 

fileds = ReflectFiledsNameByClassName(className);  //类名 找 属性名及对应的Description

filedValues = ReflectEnumFiledAndValues(typeof(SoftOrderState));// 返回  所有字段和值的键值对

 

public enum EnumWorkTypes
{
    家居住宅,
    酒店,
    别墅住宅,
    办公室,
    酒吧KTV
}

 

var types = "";
                                            var typs = typeof(EnumWorkTypes).GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
                                            foreach (var fi in typs)
                                                types += "<li value=" + fi.Name + ">" + fi.Name + "</li>";


                                            Response.Write(types);

3.正则备忘

           
var password = $("#password").val();
            if (!(/^[\w\d.,\?]{5,15}$/).test(password)) {
                alert("密码不能输入特殊字符并且长度为5到15");
                return;
            }

// 正则用  ^  跟 $ 包起来表示匹配本身 (要限制长度必须包起来) ,如果不包就表示字符串中匹配到5-15长度的一段字符,条件就为真
// 手机号码

 var mobile = $("#mobile").val();
            if (!(/^1[34578]\d{9}$/.test(mobile))) {
                alert('手机号码格式错误!');
                return ;
            }
// 生成随机字符串

 function generateAccountAndPwd() {
            var x = 1000000;
            var y = 1;
           // var rand1 = String.fromCharCode(Math.floor(Math.random() * 26) + "A".charCodeAt(0)) + String.fromCharCode(Math.floor(Math.random() * 26) + "A".charCodeAt(0)) + parseInt(Math.random() * (x - y + 1) + y) + String.fromCharCode(Math.floor(Math.random() * 26) + "A".charCodeAt(0)) + parseInt(Math.random() * (x - y + 1) + y) + String.fromCharCode(Math.floor(Math.random() * 26) + "A".charCodeAt(0));

            var rand2 = String.fromCharCode(Math.floor(Math.random() * 26) + "a".charCodeAt(0)) + parseInt(Math.random() * (x - y + 1) + y) + String.fromCharCode(Math.floor(Math.random() * 26) + "a".charCodeAt(0));
            $("#password").val(rand2);

        };

4.搜索配下拉布局

 

转载于:https://www.cnblogs.com/virtualWindGuest/p/7462441.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值