简体   繁体   English

如何使用LINQ更新具有相同值的所有记录?

[英]How can I update with LINQ all records that have equal value?

Can anybody help me with this: I want select all records from datatable which have for example sid=123 and after that save they with sid=456. 任何人都可以帮助我:我想从数据表中选择所有记录,例如sid = 123,然后用sid = 456保存它们。

How can I do this with LINQ? 如何使用LINQ执行此操作?

items.Where(i=>i.sid == 123).ToList().ForEach(i=>i.sid = 456);

or rather use normal foreach 或者更确切地说使用普通的foreach

foreach (var item in items.Where(i=>i.sid == 123))
{
    item.sid = 456
}

edit: sorry, i didn't notice that datatable. 编辑:抱歉,我没注意到那个数据表。 you can't query rows on datatable directly (they don't impletement IEnumerable) 你不能直接在数据表上查询行(它们没有实现IEnumerable)

but you can do something like this 但你可以做这样的事情

using System.Data; //System.Data.DataSetExtensions.dll
datatable.AsEnumerable().Where(row=>row.Field<int>("sid") == 1234)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM