我正在尝试为 Entity Framework 创建一个查询,该查询将允许我获取 ID 列表并更新与它们关联的字段。
SQL 示例:
UPDATE Friends
SET msgSentBy = '1234'
WHERE id IN (1, 2, 3, 4)
如何将上述内容转换为 Entity Framework ?
最佳答案
类似下面的内容
var idList=new int[]{1, 2, 3, 4};
using (var db=new SomeDatabaseContext())
{
var friends= db.Friends.Where(f=>idList.Contains(f.ID)).ToList();
friends.ForEach(a=>a.msgSentBy='1234');
db.SaveChanges();
}
更新:
您可以按如下方式更新多个字段
friends.ForEach(a =>
{
a.property1 = value1;
a.property2 = value2;
});
关于c# - 从 id 列表更新 Entity Framework 中的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21592596/