c# linq update单个字段
Posted Danlis
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# linq update单个字段相关的知识,希望对你有一定的参考价值。
1.更新单个字段
/// <summary> /// 更新字段 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="conditions">where 筛选对象</param> /// <param name="updateLamda">更新字段</param> /// <returns></returns> public IEnumerable<T> Update<T>( Expression<Func<T, bool>> conditions,Func<T, dynamic> updateLamda) where T : ModelBase { List<T> setList; if (conditions!=null) { setList= this.Set<T>().Where(conditions).ToList(); } else { setList = this.Set<T>().ToList(); } foreach (var item in setList) { SetValue<T>(item, updateLamda); this.Entry<T>(item).State = EntityState.Modified; } this.SaveChanges(); return setList; } public dynamic SetValue<T>(T entity, Func<T, dynamic> expr) { return expr(entity); }
action中的使用:
public ActionResult Edit() { using (ActivityDbContext dbContext = new ActivityDbContext()) { dbContext.Update<Order>(u => u.ID==1,u =>u.UserName= u.OrderNumber.Replace("1", "xx"));
//第一个lambda表达式是用来筛选的,第二个则是进行替换的,在方法编写里面,用了dynamic动态对象,因为没办法确定改了哪个属性,只不过暂时只能改一个,改多个还不会(-_-) } return View(); }
2.lambda修改对象的属性
public dynamic SetValue<T>(T entity, Func<T, dynamic> expr) { return expr(entity); }
添加对象自身方法
public static class UpdateObject { public static dynamic SetValue2<T>(this T obj, Func<T, dynamic> expr) { return expr(obj); } public static dynamic SetValue2<T>(this T obj, List<Func<T, dynamic>> expr) { foreach (var item in expr) { item(obj); } return obj; } }
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 6 #region 修改对象属性方法 7 GetProperty p = new GetProperty() { Name1 = "1" }; 8 //SetValue<GetProperty>(p, u => u.Name1 = "2"); 9 Console.WriteLine(p.Name1); 10 //p.SetValue2<GetProperty>(u => u.Name2 = u.Name2 + 1 + 2); 11 p.SetValue2<GetProperty>(new List<Func<GetProperty, dynamic>>() { new Func<GetProperty, dynamic>(u => u.Name2 = u.Name2 + 1), new Func<GetProperty, dynamic>(u => u.Name3 = u.Name3 + 1) }); 12 Console.WriteLine(p.Name2); 13 Console.WriteLine(p.Name3); 14 #endregion 15 Console.ReadKey(); 16 } 17 18 public static string GetPropertyName<T>(Expression<Func<T,string>> expr) 19 { 20 var name = ((MemberExpression)expr.Body).Member.Name; 21 return name; 22 } 23 24 public static dynamic SetValue<T>(T entity, Func<T, dynamic> expr) 25 { 26 return expr(entity); 27 } 28 29 public static dynamic SetValue<T>(T entity,List< Func<T, dynamic>> expr) 30 { 31 foreach (var item in expr) 32 { 33 item(entity); 34 } 35 return entity; 36 } 37 38 } 39 40 public static class UpdateObject 41 { 42 public static dynamic SetValue2<T>(this T obj, Func<T, dynamic> expr) 43 { 44 return expr(obj); 45 } 46 public static dynamic SetValue2<T>(this T obj, List<Func<T, dynamic>> expr) 47 { 48 foreach (var item in expr) 49 { 50 item(obj); 51 } 52 return obj; 53 } 54 55 } 56 57 public class GetProperty{ 58 public string Name1 { get; set; } 59 public string Name2 { get; set; } 60 public string Name3 { get; set; } 61 }
以上是关于c# linq update单个字段的主要内容,如果未能解决你的问题,请参考以下文章