使用LINQ生成没有选择直接更新(Use linq to generate direct update without select)

   IT问题网   2018-12-04 00:00:00

问 题

每个人儿真好

我还在学习linq所以请原谅我,如果这是幼稚的。当你直接处理sql,您可以生成条件语句更新命令,不运行select语句。

当我使用linq工作,我似乎遵循的方式:

  1. 选择实体
  2. 修改实体
  3. 提交修改

我想要做的是使用linq和延迟执行直接更新。直接发生的实际执行的sql而不被发送到客户端的任何数据是否有可能?

 的datacontext dc =新的datacontext

变种q =从产品dc.products
其中,product.type = 1
设置product.count = 0

dc.submitchanges
 

因此,在本质linq拥有所有需要,而无需使用选择生成的更新命令的信息。它将运行sql:

 更新产品集计数= 0其中type = 1
 

确实喜欢"设置"关键字存在linq?

解决方案

没有,既没有linq也不linq to sql的设立为基础的更新功能。

在linq to sql中,你必须查询您要更新,更新字段/属性所必需的对象,然后调用的submitchanges()。例如:

var qry =从产品dc.products那里product.name =='foobar的'选择的产品;
var项目= qry.single();
item.count = 0;
dc.submitchanges();
 

如果你希望做配料:

var qry =从产品dc.products那里product.type == 1选择的产品;
的foreach(在qry var项)
{
item.count = 0;
}
dc.submitchanges();
 

另外,您可以编写查询自己:

dc.executecommand("更新产品设置计数= 0,其中type = 1",null);
 

标签:使用生成没有选择直接更新



分享:

  • 微信
  • QQ好友
  • QQ空间
  • 新浪微博


热门推荐

是否有可能拦截控制台输出?(Is it possible to intercept Console output)

problem i call a method, say, fizzbuzz(), over which i ha ...

当使用排序列表 TKEY的,TValue在一个SortedDictionary TKEY的,TValue?(When to use a SortedList<TKey, TValue> over a SortedDictionary<TKey, TValue>)

problem this may appear to be a duplicate of this questio ...

如何初始化列表 T给定的尺寸(相对于容量)?(How to initialize a List<T> to a given size (as opposed to capacity))

problem .net offers a generic list container whose perfor ...

如何使用XPath用的XDocument?(how to use XPath with XDocument)

problem there is a similar question, but it seems that th ...

什么是WebConfigurationManager和ConfigurationManager中的区别?(What's the difference between the WebConfigurationManager and the ConfigurationManager)

problem what's the difference between the webconfiguratio ...

NUnit的测试运行秩序(NUnit Test Run Order)

problem by default nunit tests run alphabetic ...

如何恢复从C#数据库(How to restore a database from C#)

problem i have a sql 2008 db. i am running a form that ba ...

调用堆栈不说"你来自哪里"但是"你要去哪里,下一个"?(The call stack does not say "where you came from", but "where you are going next")

problem in a previous question (get object ca ...

如何读取和写入ID3标签在C#中的MP3?(How to read and write ID3 tags to an MP3 in C#)

problem is there a library for reading and writing id3 ta ...

如何设置读取权限的X.509证书从.NET私钥文件(How to set read permission on the private key file of X.509 certificate from .NET)

problem here is the code to add a pfx to the cert store. ...

获取列表视图的项目双击事件(Get the item doubleclick event of listview)

problem how to get the item double click event of listvie ...

MongoDB的交易?(MongoDB transactions)

problem playing around with mongodb and norm in .net. th ...

确定是否.NET程序集是从相同的源代码构建(Determine whether .NET assemblies were built from the same source)

problem does anyone know of a way to compare two .net ass ...

如何prevent更新一个ListViewItem的的文字时,在闪烁的ListView?(How to prevent flickering in ListView when updating a single ListViewItem's text)

problem all i want is to update an listviewit ...

谁应该调用Dispose IDisposable的对象时传递到另一个对象?(Who should call Dispose on IDisposable objects when passed into another object)

problem is there any guidance or best practic ...

获得一天的后缀使用则DateTime.ToString时,()(Getting day suffix when using DateTime.ToString())

problem is it possible to include the day suffix when for ...

改变目标框架我在Visual Studio解决方案的所有项目(Change the Target Framework for all my projects in a Visual Studio Solution)

problem i need to change the target framework ...

string.IsNullOrEmpty(串)与string.IsNullOrWhiteSpace(串)(string.IsNullOrEmpty(string) vs. string.IsNullOrWhiteSpace(string))

problem is use of string.isnullorempty(string ...

上传文件使用C#到ftp(Upload file to ftp using c#)

problem i try upload a file to an ftp-server with c#. the ...

什么是App.config中呢?(What is app.config for)

problem settings.settings generates settings.designer.cs ...