linq to entity常用操作
2023-04-18 14:42:31 时间
一、聚合函数查询
double sum = 0; using (xxxEntities db = new xxxEntities()) { sum = db.userinfo.AsNoTracking().Where(d => d.id==id).Sum(m => (double?)m.money).GetValueOrDefault(); } return sum;
二、删除操作
int result = 0; if (id > 0) { using (gghdbEntities db = new gghdbEntities()) { var data = db.quote.Where(d => d.id == id).FirstOrDefault(); db.quote.Remove(data); db.SaveChanges(); result = 1; } } return result;
三、执行事物(Net4.0以上版本支持)
using (xxxEntities db = new xxxEntities()) { using (TransactionScope transaction = new TransactionScope()) { //操作,出错自动回滚 //提交事务 transaction.Complete(); } }
需要添加引用:using System.Transactions;
相关文章
- 技术面试官如何甄别培训班学员
- c++11:nlohmann::json进阶使用(一)ordered_json
- c++11:nlohmann::json进阶使用(三)使用basic_json模板类
- 三分钟了解 npm 库升级最佳实践
- 三分钟了解 yarn.lock/package-lock.json
- Python-ini文件-02-删及写操作
- 实战技能分享,如何让工程代码各种优化等级通吃,含MDK AC5,AC6,IAR和GCC
- Leetcode No.49 字母异位词分组
- Leetcode No.48 旋转图像
- Leetcode No.51 N皇后(DFS)
- Leetcode No.52 N皇后 II(DFS)
- Leetcode No.55 跳跃游戏(贪心)
- Leetcode No.59 螺旋矩阵 II
- Leetcode No.71 简化路径(栈)
- 【网站搭建】腾讯云服务器购买及域名解析及宝塔安装
- Leetcode No.72 编辑距离(动态规划)
- Leetcode No.73 矩阵置零(C++版)
- Leetcode No.74 搜索二维矩阵(二分查找)
- 为 K8s workload 引入的一些 BPF datapath 扩展
- Leetcode No.75 颜色分类