在更新数据库中的多个记录使用LINQ没有循环SQL中,这可能吗? [英] updating multiple records in database with linq to sql without loop, is it possible?

查看:135
本文介绍了在更新数据库中的多个记录使用LINQ没有循环SQL中,这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,让说我想一次更新数据库的多行,不用循环,如果我们用循环做到这一点,它可能是这样的:

ok, let say i want to update multiple rows in database at once, without looping, if we do it with loop, it might look like this :

var products = (from prod in context.Products
               where prod.ProductCategoryID == myProductID
               select prod).ToList();


// modify each object
foreach (var product in products)
{
    product.PhoneNumber = ???;
    product.Name = ???;
    product.Address = ???;
}


context.SubmitChanges();

有没有更好的方式来做到这一点?没有做任何循环?我认为作为一个LINQ初学者,所以如果有一些更好的方法,如果你给我提供了一些例子,在此先感谢这将是巨大的。

is there better way to do this? without doing any loop? consider me as a beginner on linq, so if there is some better way, it would be great if you provide me some example, thanks in advance.

推荐答案

LINQ 是quering记录。您当前的的foreach 循环更具可读性,如果你尝试其他的方法那么将在内部使用循环。

LINQ is for quering records. Your current foreach loop is more readable and if you try other approach then it will use loop internally.

您可以看到它修改使用LINQ收集这个答案,但它是不推荐的方式。

You can see this answer which modifies the collection using LINQ, but it is not the recommended way.

这篇关于在更新数据库中的多个记录使用LINQ没有循环SQL中,这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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