使用LINQ更新记录 [英] Update records using Linq

查看:142
本文介绍了使用LINQ更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要设置在一个表中的值的行的子集。在SQL中,我会做这样的:

 更新dbo.Person SET is_default = 0,其中为person_id = 5 

有没有办法做到这一点的LINQ?



我目前使用:

  VAR的结果=(从Context.People p ....)

标记。



有没有我可以使用更新的方法?还是我必须把所有的记录,那么更新它们一个接一个在foreach?



这是最有效的方式,如果这甚至可能吗?

 (从Context.person_account_portfolio p其中p.person_id == PERSONID选择p).ToList()
。的ForEach(
X =>
x.is_default =
假);


解决方案

这个工作最好的。

 (从Context.person_account_portfolio 
p其中p.person_id == PERSONID选择p).ToList()
.ForEach(X = GT; x.is_default = FALSE);


I need to set a value in a table for a subset of rows. In SQL, I would do this:

UPDATE dbo.Person SET is_default = 0 WHERE person_id = 5

Is there a way to do this in Linq?

I currently use the:

var result = (from p in Context.People....)

notation.

Is there an update method I can use? Or do I have to get all the records, then update them one-by-one in a Foreach?

Is this the most efficient way, if this is even possible?

    (from p in Context.person_account_portfolio where p.person_id == personId select p).ToList()
                                                                                       .ForEach(
                                                                                           x =>
                                                                                           x.is_default =
                                                                                           false);

解决方案

This worked best.

(from p in Context.person_account_portfolio 
 where p.person_id == personId select p).ToList()
                                        .ForEach(x => x.is_default = false);

这篇关于使用LINQ更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆