当前位置: 首页 > news >正文

对实体类Id自增

我们首先需要明白

lambda 表达式  (item=>item.Id)↓
Expression 树   →  检查是属性访问↓
Expression.Assign → 拼出 “item.Id = index”↓
Compile() → 生成机器码委托  Action<T,int>↓
放进 ConcurrentDictionary 缓存,终身复用

在原生的Linq中,我们可以写

List<Student> Students=CreateStudent();
Students.Select((item, index) => { item.Id = index; return item; });

我们可以自定义一个

Students.SelectWithIndex(item=>item.Id);
  /// <summary>/// 对序列按索引赋值,指定成员表达式 item => item.Id/// </summary>public static IEnumerable<T> SelectWithIndex<T>(this IEnumerable<T> source, Expression<Func<T, int>> memberSelector){if (source == null) throw new ArgumentNullException(nameof(source));if (memberSelector == null) throw new ArgumentNullException(nameof(memberSelector));// 编译一次 settervar setter = GetOrCreateSetter(memberSelector);return source.Select((item, idx) =>{setter(item, idx);return item;});}// 内部:把 Expression<item.Id> 编译成 Action<item,index>private static readonly ConcurrentDictionary<(Type, string), Delegate> _cache = new ConcurrentDictionary<(Type, string), Delegate>();
// ① 这是一个“编译并缓存”的工厂方法:输入 lambda (item=>item.Id) ,输出 Action<T,int> 委托
//    泛型 T 就是你要处理的模型类型,例如 MotionCalibrationModel
private static Action<T, int> GetOrCreateSetter<T>(Expression<Func<T, int>> selector)
{// ② 拿到 T 的运行时类型信息,后面要用它拼缓存键var type = typeof(T);// ③ 拼一个复合键:(类型, lambda 主体字符串)//    例如 (MotionCalibrationModel, "item.Id")  这样同一个类不同属性也能分开缓存var key = (type, selector.Body.ToString());// ④ ConcurrentDictionary 的 GetOrAdd 是线程安全的“获取或添加”//    如果缓存里已有这个键,直接返回已编译好的委托;否则执行 lambda 表达式编译逻辑return (Action<T, int>)_cache.GetOrAdd(key, _ =>{/* ⑤ 下面是“编译”过程,只做一次,以后复用 */// ⑥ 确保 lambda  body 是 item.Id  这种“取属性”形式if (!(selector.Body is MemberExpression mem) ||!(mem.Member is PropertyInfo prop) ||!prop.CanWrite ||                 // 必须有 set 访问器prop.PropertyType != typeof(int)) // 必须是 intthrow new ArgumentException("Lambda 必须返回一个可写的 int 属性,如 item => item.Id");// ⑦ 手工构造两个参数表达式//    paramItem  代表“将来传进来的对象”//    paramIndex 代表“将来传进来的索引值”var paramItem = Expression.Parameter(type, "item");var paramIndex = Expression.Parameter(typeof(int), "index");// ⑧ 构造赋值表达式:  item.Id = index//    左侧是“取属性”,右侧是“索引值”var assign = Expression.Assign(Expression.Property(paramItem, prop), paramIndex);// ⑨ 把赋值表达式包成 Lambda ://    (item, index) => item.Id = index//    Compile() 之后变成真正的委托 Action<T,int>return Expression.Lambda<Action<T, int>>(assign, paramItem, paramIndex).Compile();});
}
http://www.zskr.cn/news/8548.html

相关文章:

  • 深入解析:InnoDB存储引擎-锁
  • 20231326王荣盛《密码系统设计》第二周预习报告
  • Gitflow 工作流程
  • 魔改chromium真正通杀全网debugger检测
  • 【截稿倒计时、高录用、稳检索】2025年教育创新与信息技术国际学术会议(EIIT 2025)
  • hashcat高效爆破Wi-Fi密码方法(比aircrack-ng快)
  • 更新到macOS Sequoia后,chrome无法用ip访问192.168.*
  • Typora标题自动显示序号,大纲中也显示序号
  • 【IEEE出版、格林威治大学主办】第六届信息科学与并行、分布式处理国际学术会议(ISPDS 2025)
  • 【2025-09-18】工作情绪
  • Ubuntu 系统部署 LNMP 环境详细教程(附shell脚本一键部署↓) - 指南
  • 详细介绍:7HTMLCSS高级
  • C++学习笔记之输入输出流 - 教程
  • 使用Inno Setup打包安装程序exe流程
  • XXL-JOB-源码分享(1)
  • WPF 字符竖向排列的排版格式(直排)表明控件
  • 深入解析:HSA35NV001美光固态闪存NQ482NQ470
  • YOLO实战应用 1YOLOv5 架构与模块
  • SpringBoot整合RustFS:全方位优化文件上传性能
  • windows使用es-client插件
  • AI学习日记 - 实践
  • es中的索引
  • VIVADO的IP核 DDS快速采用——生成正弦波,线性调频波
  • 深入解析:C语言---判断语句
  • YOLO进阶提升 4训练准备与数据处理
  • YOLO进阶提升 5标注与配置
  • 【学术会议前沿信息|科研必备】IEEE/EI/Scopus三检护航!人工智能+自动化控制+人文社科+遥感+地理信息+视觉领域国际会议征稿启动,硕博生速来! - 教程
  • YOLO进阶提升 3YOLOv4 改进
  • 深入解析:数据库入门实战版
  • C# Avalonia 15- Animation- AnimationPlayerTest