C# EF
#region 16-批量删除 /// <summary> /// 16-批量删除 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="delWhere">删除条件</param> /// <returns></returns> public async Task<int> BatchDeleteAsync<T>(Expression<Func<T, bool>> delWhere) where T : class { int count = await db.Set<T>().Where(delWhere).ExecuteDeleteAsync(); return count; } #endregion #region 17-批量更新 /// <summary> /// 17-批量更新 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="updateWhere">更新条件</param> /// <param name="setPropertyCalls">更新实体表达式</param> /// <returns></returns> public async Task<int> BatchUpdateAsync<T>(Expression<Func<T, bool>> updateWhere, Expression<Func<SetPropertyCalls<T>, SetPropertyCalls<T>>> setPropertyCalls) where T : class { int count = await db.Set<T>().Where(updateWhere).ExecuteUpdateAsync(setPropertyCalls); return count; } #endregion
#region 01-批量插入 /// <summary> /// 01-批量插入 /// </summary> /// <typeparam name="T">实体类型</typeparam> /// <param name="list">实体集合</param> /// <returns></returns> public async Task InsertBulkAsync<T>(List<T> list) where T : class { await db.BulkInsertAsync(list); } #endregion
#region 01-批量更新 { int count = await baseService.BatchUpdateAsync<T_SysErrorLog>(u => u.addTime < DateTime.Now, u => u.SetProperty(u => u.delFlag, 1) .SetProperty(u => u.userAccount, u => u.userAccount + "00000") ); } #endregion #region 02-批量删除 { int count = await baseService.BatchDeleteAsync<T_SysErrorLog>(u => u.delFlag == 1); } #endregion
EF增删改查
参考:
https://www.cnblogs.com/zry2510/p/6209118.html
EF查询表的某个字段的最大值
var a=db.表名.Select(s=>s.列名).Max();
EF查询表是否表中已存在某个值
var a=db.表名.Any(s=>s.列名==传入的值);
if(a){ 已存在 }
或者
from x in a where !b.Any(y=>y.id==x.id) select x;
EF 查询所有字段
var query=db.表名.Where(r=> r.Id==Id).Select(p=>p).ToList();
EF 查询部分字段
var query=db.表名.Where(r=> r.Id==Id).Select(g=>new{d.Id, d.想要的字段}).ToList();
EF修改部分字段
参考:
https://www.cnblogs.com/jhxk/articles/9719606.html
https://www.cnblogs.com/zxsn/p/12658355.html
EF 链表查询
参考:
https://www.cnblogs.com/tinya/p/4623503.html
https://www.cnblogs.com/renyan52099/p/14781372.html
http://www.voidcn.com/article/p-rqfusmvr-bqh.html
查看EF生成的SQL语句
参考:
https://www.cnblogs.com/cxxtreasure/p/13382726.html
EF执行Sql查询
参考:
https://www.cnblogs.com/cxxtreasure/p/13382695.html
EF + Lambda分页
参考:
https://blog.csdn.net/xoofly/article/details/84317408
EF + LINQ查询前N条记录
参考:
https://blog.csdn.net/ptyzhu/article/details/7893563
linq GroupBy 多字段分组
参考:
https://www.cnblogs.com/tianyang1027/p/13931811.html
C#EF保存多个记录
using(var db = context) { var dataToUpdate = db.Employees.ToList(); var department= "IT"; dataToUpdate.ForEach(x=>{ x.department = department; }); db.SaveChanges(); }
https://www.cnblogs.com/yaopengfei/p/7678755.html
结合EF的本地缓存属性来介绍【EF增删改操作】的几种形式
https://www.cnblogs.com/yaopengfei/p/7674715.html
本地缓存、立即加载、延迟加载(不含导航属性)
https://www.cnblogs.com/yaopengfei/p/7701481.html
EF的三种事务的应用场景和各自注意的问题(SaveChanges、DBContextTransaction、TransactionScope)
https://www.cnblogs.com/yaopengfei/p/7748221.html
泛型(泛型类、接口、方法、委托、泛型约束、泛型缓存、逆变和协变)
https://www.cnblogs.com/yaopengfei/p/6880629.html