博客
关于我
List GroupBy真实用法,Reflection(反射)用法,Enum用法,正则,搜索下拉布局
阅读量:796 次
发布时间:2023-02-05

本文共 3057 字,大约阅读时间需要 10 分钟。

编程技巧实用分享

1. GroupBy 运算的实际应用

GroupBy 是 LINQ 中一个非常实用的方法,用于对集合数据进行分组操作。以下是一个实际场景的 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
(); temp.Products.AddRange(item); roomedProducts.Add(temp);}

这个代码 snippet 展示了如何根据房间名称对产品列表进行分组,并将每个分组的数据存储到 roomedProducts 列表中。


2. 枚举类与反射的结合使用

为了避免在代码中反复出现类似于 order.state == 50order.statedesc == "已付款" 的情况,可以通过枚举类配合反射来实现状态管理。以下是一个枚举类和反射方法的示例:

public enum DesignOrderState{    ///     /// 待付款    ///     [Description("待付款")]    WaitPaying = 30,    ///     /// 已付款    ///     [Description("已付款")]    Paid = 50,    ///     /// 设计中    ///     [Description("设计中")]    Designing = 70,    ///     /// 已交付    ///     [Description("已交付")]    Completed = 90,}public static Dictionary
ReflectFiledsNameByClassName(string className){ Dictionary
result = new Dictionary
(); 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;}public static Dictionary
ReflectEnumFiledAndValues(Type type){ Dictionary
result = new Dictionary
(); FieldInfo[] properties = type.GetFields(); foreach (var item in properties) { try { result.Add(item.Name, (int)(Enum.Parse(type, item.Name))); } catch (Exception) { // 忽略不能枚举的字段 } } return result;}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;}

这个代码 snippet 展示了如何通过反射获取枚举类的字段名及其对应的描述信息,以及如何从枚举类中获取字段值。


3. 正则表达式的实用技巧

在前端开发中,正则表达式是处理文本验证的重要工具。以下是一些常用的正则表达式示例:

var password = $("#password").val();if (!(/^[\w\d.,\?]{5,15}$/).test(password)){    alert("密码不能输入特殊字符并且长度为5到15");    return;}// 手机号码验证var mobile = $("#mobile").val();if (!(/^1[34578]\d{9}$/.test(mobile))){    alert('手机号码格式错误!');    return;}

这个代码 snippet 包含了两个常用的正则表达式:

  • ^[\w\d.,\?]{5,15}$ 用于验证密码字符,允许字母、数字、特殊字符(如逗号、问号)在 5 到 15 个字符之间。
  • ^1[34578]\d{9}$ 专门用于验证手机号码格式,确保手机号以 1、3、4、7、8 开头,后面跟着 9 位数字。

  • 4. 搜索引擎友好的下拉布局

    以下是一个简单的搜索下拉布局示例,供开发者参考:

    这个下拉框可以用于搜索页面中的内容分类,用户可以根据需要从多个类型中选择。


    以上就是本次编程技巧分享的内容,希望对你有所帮助!如果需要更多实用技巧,欢迎继续关注我的博客。

    转载地址:http://yzufk.baihongyu.com/

    你可能感兴趣的文章
    LiveGBS user/save 逻辑缺陷漏洞复现(CNVD-2023-72138)
    查看>>
    localhost:5000在MacOS V12(蒙特利)中不可用
    查看>>
    logstash mysql 准实时同步到 elasticsearch
    查看>>
    Luogu2973:[USACO10HOL]赶小猪
    查看>>
    mabatis 中出现&lt; 以及&gt; 代表什么意思?
    查看>>
    Mac book pro打开docker出现The data couldn’t be read because it is missing
    查看>>
    MAC M1大数据0-1成神篇-25 hadoop高可用搭建
    查看>>
    mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
    查看>>
    Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
    查看>>
    MangoDB4.0版本的安装与配置
    查看>>
    Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
    查看>>
    mapping文件目录生成修改
    查看>>
    MapReduce程序依赖的jar包
    查看>>
    mariadb multi-source replication(mariadb多主复制)
    查看>>
    MariaDB的简单使用
    查看>>
    MaterialForm对tab页进行隐藏
    查看>>
    Member var and Static var.
    查看>>
    memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
    查看>>
    memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
    查看>>
    Memcached:Node.js 高性能缓存解决方案
    查看>>