求LinQ 更新字段语法

jiajuchang00 2011-12-07 09:20:17
SQL

update Table set col1 = '123' where id = 3


怎样把他转换为LinQ语法,在一个List<Entity>集合里面.不是LinQ to Sql的,我只是想把一个对象里面某个字段的值做一下修改。
...全文
355 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
岁纳Tomato 2013-10-11
  • 打赏
  • 举报
回复
引用 7 楼 tenshimao 的回复:
[quote=引用 6 楼 yxb1987 的回复:] [Quote=引用 2 楼 dongxinxi 的回复:] List<Entity>.ForEach(e=> {if(e.id==3)e.col1="123";}); [/Quote]这个好,我用的这个
一点都好[/quote] 打错了 一点都不好
岁纳Tomato 2013-10-11
  • 打赏
  • 举报
回复
引用 6 楼 yxb1987 的回复:
[Quote=引用 2 楼 dongxinxi 的回复:] List<Entity>.ForEach(e=> {if(e.id==3)e.col1="123";}); [/Quote]这个好,我用的这个
一点都好
神都码农 2011-12-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 dongxinxi 的回复:]
List<Entity>.ForEach(e=> {if(e.id==3)e.col1="123";});
[/Quote]这个好,我用的这个
q107770540 2011-12-08
  • 打赏
  • 举报
回复
更新list?
andyalex 2011-12-08
  • 打赏
  • 举报
回复
一般都要先取出entity,赋值,再保存更新;不然就写sql语句,写可以了
gzalrj 2011-12-08
  • 打赏
  • 举报
回复
写一个扩展,大概思路是这样的,发我在实际中的转化
  static class UpdateExtensions
{
private static Command GetCommands<Entity>(Expression<Func<Entity, bool>> Predicate, Expression<Func<Entity, Entity>> Updater) where Entity : class,IEntity
{
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;
Type type = typeof(Entity);
var tableAttribute = type.GetCustomAttributes(false).OfType<System.ComponentModel.DataAnnotations.TableAttribute>().FirstOrDefault();
string Table = tableAttribute == null ? type.Name : tableAttribute.Name;
if (Table.Length > 2 && Table.StartsWith("DB", StringComparison.CurrentCultureIgnoreCase))
{
Table = Table.Substring(2, Table.Length - 2);
}
var PreTable = System.Configuration.ConfigurationManager.AppSettings["PreTable"];
Table = PreTable == null ? Table : PreTable + "_" + Table;
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}", Table, sqlUpdateBlock, sqlCondition);
//获取SQL参数数组 (包括查询参数和赋值参数)
var args = Builder.Arguments.Union(updateMemberCollection.Select(c => c.Value)).ToArray();
return new Command() { Text = commandText, args = args };
}
/// <summary>
/// 执行UpDate返回影响的条数
/// </summary>
public static int UpdateEntity<Entity>(this EFDbContext<Entity> Context, Expression<Func<Entity, bool>> Predicate, Expression<Func<Entity, Entity>> Updater) where Entity : class,IEntity
{
Command com = GetCommands<Entity>(Predicate, Updater);
int Result = Context.Database.ExecuteSqlCommand(com.Text, com.args);
return Result;
}

然后调用 再写一个方法


public int Update(Expression<Func<TEntity, bool>> Predicate, Expression<Func<TEntity, TEntity>> Updater)
{
return _DbContent.UpdateEntity<TEntity>(Predicate, Updater);
}

最后调用
DB<DbBook> v = new DB<DbBook>();
int num=v.Update(f => f.Id ==1, d => new DbBook() { Name = "abc" }

生成的SLQ为
Update [Book] Set [Name]='abc' Where ([Id] = 1)
当然,你的是LIST,可以将上面的扩展修改成你要的
  • 打赏
  • 举报
回复
List<Entity>.ForEach(e=> {if(e.id==3)e.col1="123";});
阿非 2011-12-07
  • 打赏
  • 举报
回复
(from e in List<Entity>
where e.id==3
select e).FirstOrDefault().coll="123";

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧