使用linq更新datatable中的字段值 [英] Update a field value in datatable using linq
问题描述
嗨
在Result表中我只返回tableA,但是使用dt2 NewSales值更新了OldSales值
In Result table i am returning the tableA only but the OldSales value updated with dt2 NewSales value
以上结果我正在使用下面的Linq查询
for the above result I am Using the below Linq Query
Dim dt as DataTable=Nothing
dt=(From n In dt1.AsEnumerable Join m In dt2.AsEnumerable On n.Field(Of Integer)("Id") Equals m.Field(Of Integer)("Id")
Select New With {
.Id=n.Field(Of Integer)("Id") ,
.Brand=n.Field(Of String)("Brand") ,
.OldSales=n.Field(Of Integer)("NewSales")
}).CopyToDataTable()
但在我的实际中表我有超过15列。所以,我必须列出select子句中的所有dt1列,因为用dt2 OldValues更新了一个已归档的OldSales值
but in my actual table I have more than 15 columns. So, i have to list all dt1 columns in select clause because of updating one filed OldSales value with dt2 OldValues
在我的应用程序中我在很多地方得到这个
In My Application i am getting this in many places
所以有任何简单的方法来返回数据而不列出更新数据表中一个字段的所有列。
so is any easy way to return the data without listing the all columns for updating one filed in the datatable.
我搜索了解决方案,我看到一些查询,如使用foreq扩展方法到linq查询,但我无法实现请求帮助。
i googled for the solution and i saw some query like using foreach extension methods to the linq queries but i am not able to implement please help.
推荐答案
这是ADO.NET实体框架和Linq to Entity论坛。你在说Linq到Datatable。我觉得需要有一个针对您的情况的Linq General论坛,讨论使用Linq的所有形式。有用这样的MSDN论坛几年
回来了。  也许,您应该停止使用数据表并使用自定义对象的List(T)。
This is the ADO.NET Entity Framework and Linq to Entity forum. You are talking Linq to Datatable. I feel that there needs to be a Linq General forum for your situation that discusses all forms of using Linq. There use to be such a MSDN forum a few years back. Maybe, you should stop using datatables and use a List(of T) of custom objects instead.
也许在您的情况下,您只需将ADO.NET与SQL Command对象和T-一起使用您需要更新的数据库表列的SQL Update语句。这样,您不必处理数据表中的所有列,只需更新数据库表中的选定列
行。
And maybe in your case, you just use ADO.NET with SQL Command object and T-SQL Update statement for the database table columns you need to update. That way you don't have to address all the columns in a datatable just to update selected columns in a DB table row.
这篇关于使用linq更新datatable中的字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!