一个关于linq的操作类
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /// <summary> 2 /// Linq通用数据访问类 3 /// 指定TDataBase来代替后面要使用的数据上下文(指代) 4 /// where:说明指代的类型 5 /// new:限定必须有一个不带参数的构造函数 6 /// </summary> 7 /// <typeparam name="TDataBase"></typeparam> 8 public class LinqHelper<TDataBase> where TDataBase : DataContext, new() 9 { 10 private static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); 11 TDataBase db = null; 12 /// <summary> 13 /// 创建数据库连接 14 /// </summary> 15 public LinqHelper() 16 { 17 db = new TDataBase(); 18 db.Connection.ConnectionString = connectionString; 19 } 20 21 /// <summary> 22 /// 获取所有数据 23 /// </summary> 24 /// <typeparam name="T"></typeparam> 25 /// <returns></returns> 26 public List<T> GetList<T>() where T : class 27 { 28 return db.GetTable<T>().ToList(); 29 } 30 31 /// <summary> 32 /// 按条件查询 33 /// </summary> 34 /// <typeparam name="T"></typeparam> 35 /// <param name="predicate">Lambda表达式</param> 36 /// <returns></returns> 37 public List<T> GetList<T>(Expression<Func<T, bool>> predicate) where T : class 38 { 39 return db.GetTable<T>().Where(predicate).ToList(); 40 } 41 42 /// <summary> 43 /// 获取实体 44 /// </summary> 45 /// <typeparam name="T"></typeparam> 46 /// <param name="predicate"></param> 47 /// <returns></returns> 48 public T GetEntity<T>(Expression<Func<T, bool>> predicate) where T : class 49 { 50 return db.GetTable<T>().Where(predicate).FirstOrDefault(); 51 } 52 53 /// <summary> 54 /// 添加实体 55 /// </summary> 56 /// <typeparam name="T"></typeparam> 57 /// <param name="entity"></param> 58 public void InsertEntity<T>(T entity) where T : class 59 { 60 try 61 { 62 //将对象保存到上下文当中 63 db.GetTable<T>().InsertOnSubmit(entity); 64 //提交更改 65 db.SubmitChanges(); 66 } 67 catch (Exception ex) 68 { 69 throw new Exception(ex.Message); 70 } 71 } 72 73 /// <summary> 74 /// 修改实体 75 /// </summary> 76 /// <typeparam name="T"></typeparam> 77 /// <param name="entity"></param> 78 public void UpdateEntity<T>(T entity) where T : class 79 { 80 try 81 { 82 //将新实体附加到上下文 83 db.GetTable<T>().Attach(entity); 84 //刷新数据库 85 db.Refresh(RefreshMode.KeepCurrentValues, entity); 86 //提交更改 87 db.SubmitChanges(ConflictMode.ContinueOnConflict); 88 } 89 catch (Exception ex) 90 { 91 throw new Exception(ex.Message); 92 } 93 } 94 95 /// <summary> 96 /// 删除实体 97 /// </summary> 98 /// <typeparam name="T"></typeparam> 99 /// <param name="predicate"></param> 100 public void DeleteEntity<T>(Expression<Func<T, bool>> predicate) where T : class 101 { 102 try 103 { 104 //获取要删除的实体 105 var entity = db.GetTable<T>().Where(predicate).FirstOrDefault(); 106 if (entity == null) return; 107 db.GetTable<T>().DeleteOnSubmit(entity); 108 db.SubmitChanges(ConflictMode.ContinueOnConflict); 109 } 110 catch (Exception ex) 111 { 112 throw new Exception(ex.Message); 113 } 114 } 115 }
很久很久以前,谎言和真实在河边洗澡。谎言先洗好,穿了真实的衣服离开,真实却不肯穿谎言的衣服。后来,在人们的眼里,只有穿着真实衣服的谎言,却很难接受赤裸裸的真实。