如何无需加载整个行更新LINQ一列? [英] How to update a single column in LINQ without loading the entire row?

查看:188
本文介绍了如何无需加载整个行更新LINQ一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在LinqToSql,很可爱容易加载行,更改列,并提交对数据库的更改:

 使用(WDC MyDataContext =新MyDataContext())
{
文章文章= wdc.Article.First(p => p.ID == ID);
article.ItemsInStock = itemsinstock;
wdc.SubmitChanges();
}



唯一的缺点:第一条是巨大的。 要加载整篇文章,只是为了更新一列的方式矫枉过正并减慢我的应用程序显著。



有没有办法更新?使用LINQ,而无需加载整个行单列



现在我恢复使用ExecuteCommand在速度的本质,但这是丑陋且容易出错:

  wdc.ExecuteCommand(UPDATE SET条= ItemsInStock 1 @ WHERE ID = @ 2,itemsinstock,身份证); 


解决方案

ligget78给了我另一种思路如何使的更新一列:



只是这种更新的创建一个新的DataContext,只包括需要的列到这个DataContext的



此方式,不需要的列甚至不会被加载,当然不会被发送回数据库。


In LinqToSql, it is lovely easy to load a row, change a column, and submit the changes to the database:

using (MyDataContext wdc = new MyDataContext())
{        
  Article article = wdc.Article.First(p => p.ID == id);
  article.ItemsInStock = itemsinstock;
  wdc.SubmitChanges();
}

The only drawback: Article is huge. To load the entire article, just to update one column is way overkill and slows down my app significantly.

Is there a way to update a single column using LINQ, without having to load the entire row?

Right now I revert to using ExecuteCommand where speed is of essence, but this is ugly and error prone:

wdc.ExecuteCommand("UPDATE Article SET ItemsInStock = @1 WHERE ID = @2", itemsinstock,id);

解决方案

ligget78 gave me another idea how to make an update of a single column:

Create a new DataContext just for this kind of update, and only include the needed columns into this DataContext.

This way the unneeded columns will not even be loaded, and of course not sent back to the database.

这篇关于如何无需加载整个行更新LINQ一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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