日期:2014-05-20 浏览次数:20927 次
var list = BaseBizService.BOSCenterContent.ProductCategories.Where(p => p.ProductCategoryID.StartsWith(param.CategoryID)).ToList(); foreach (var item in list) { var cgModel = BaseBizService.BOSCenterContent.ProductCategories.Single(p => p.ProductCategoryID == item.ProductCategoryID); cgModel.IsPublished = true; } BaseBizService.BOSCenterContent.SaveChanges();
/// <summary> /// 更新满足条件的实体,返回更新实体的条数 /// </summary> /// <typeparam name="T">更新的类型</typeparam> /// <param name="Entity">实体类</param> /// <param name="Predicate">更新的条件</param> /// <param name="Updater">更新的值</param> /// <returns>int</returns> public static int UpdateEntity<T>(this ObjectSet<T> Entity, Expression<Func<T, bool>> Predicate, Expression<Func<T, T>> Updater) where T : class { ConditionBuilder Builder = new ConditionBuilder(); Builder.Build(Predicate.Body); string sqlCondition = Builder.Condition; //获取Update的赋值语句 var updateMemberExpr = (MemberInitExpression)Updater.Body; var updateMemberCollection = updateMemberExpr.Bindings.Cast<MemberAssignment>().Select(c => new { Name = c.Member.Name, Value = ((ConstantExpression)c.Expression).Value }); int i = Builder.Arguments.Length; string sqlUpdateBlock = string.Join(", ", updateMemberCollection.Select(c => string.Format("[{0}]={1}", c.Name, "{" + (i++) + "}")).ToArray()); string commandText = string.Format("Update {0} Set {1} Where {2}", Entity.EntitySet.Name, sqlUpdateBlock, sqlCondition); //获取SQL参数数组 (包括查询参数和赋值参数) var args = Builder.Arguments.Union(updateMemberCollection.Select(c => c.Value)).ToArray(); var Result = Entity.Context.ExecuteStoreCommand(commandText, args); return Result; }
------解决方案--------------------
是别人(好像是国外人)写的一个类,具体可以加我Q(252377274),我发给你完整的类