-
Sep 21st, 2010, 06:35 AM
#1
Thread Starter
Junior Member
LINQ datatable Update
Hello !
I'm new to LINQ area and I discovered the compact mode of this instructions vs usual code.
I found sample code for selecting, etc. from a datatable, transforming a column in a list or array - all operations are "Select"s - readonly.
I need to make an simple update on a certain rows (using LINQ) from a DISCONNECTED datatable for which now I'm using this code:
Code:
for each dr as datarow in dtable.Rows
dr("MyField") = 77
next
Its possible to do this with one line of code (using LINQ) ?
Thank you !
-
Sep 21st, 2010, 08:23 AM
#2
Re: LINQ datatable Update
vb.net Code:
Array.ForEach(dtable.AsEnumerable().ToArray(), Sub(row) row("MyField") = 77)
-
Sep 21st, 2010, 08:37 AM
#3
Re: LINQ datatable Update
That works in 2010... for earlier versions, you'll need to replace "sub" with "function"
-tg
-
Sep 21st, 2010, 09:19 AM
#4
Thread Starter
Junior Member
Re: LINQ datatable Update
-
Sep 21st, 2010, 09:27 AM
#5
Thread Starter
Junior Member
Re: LINQ datatable Update
in fact
Code:
dtable.AsEnumerable
says that
"AsEnumerable is not member of System.Data.Datatable"
(I'm using LinqBridge with vs2008 with Framework 2.0)
but your solution move me on the correct path. I'm using:
Code:
Array.ForEach(dtable.Rows.Cast(Of DataRow).ToArray, Function(dr) dr("MyField") = 77)
-
Sep 21st, 2010, 09:29 AM
#6
Re: LINQ datatable Update
Originally Posted by techgnome
That works in 2010... for earlier versions, you'll need to replace "sub" with "function"
-tg
Not true, in VS2008 you can't do it in one line. In fact, if you switched it from "Sub" to "Function" and had Option Strict On, you would get a compile error:
Code:
Option Strict On disallows operands of type Object for operator '='.
Use the 'Is' operator to test for object identity.
The reason this happens is because the "Sub" keyword in VS2010 allows you to define a lambda method that does not return a value, whereas the "Function" keyword (in this case) will try to return a Boolean value.
You could do it in VS2008 like so, however it clearly isn't one line:
vb.net Code:
Array.ForEach(dtable.AsEnumerable().ToArray(), AddressOf Me.ProcessRow)
Private Sub ProcessRow(ByVal row As DataRow)
row("MyField") = 77
End Sub
-
Sep 21st, 2010, 09:32 AM
#7
Thread Starter
Junior Member
Re: LINQ datatable Update
I found the solution - view the previous post !
Thank you !
-
Sep 21st, 2010, 09:35 AM
#8
Thread Starter
Junior Member
Re: LINQ datatable Update
Sorry - you have right ! An error is rising !
-
Sep 21st, 2010, 09:36 AM
#9
Re: LINQ datatable Update
Originally Posted by tratak
in fact
Code:
dtable.AsEnumerable
says that
"AsEnumerable is not member of System.Data.Datatable"
(I'm using LinqBridge with vs2008 with Framework 2.0)
but your solution move me on the correct path. I'm using:
Code:
Array.ForEach(dtable.Rows.Cast(Of DataRow).ToArray, Function(dr) dr("MyField") = 77)
Originally Posted by tratak
I found the solution - view the previous post !
Thank you !
This isn't going to do what you think it will. Read my post.
-
Sep 21st, 2010, 09:38 AM
#10
Re: LINQ datatable Update
1) ForumAccount - touche! Didn't quite catch that.
2) That's why we have the versions at the top of the threads... had you specified this was for 2008, you might have gotten a solution faster.
3) Now that the problem is resolved, could you please mark the thread as resolved? It's under the Thread Tools menu at the top.
-tg
-
Sep 21st, 2010, 09:43 AM
#11
Thread Starter
Junior Member
Re: LINQ datatable Update
- ok - sorry
- the problem is not quite resolved, as you can see (or the "is impossible" is the solution). Maybe someone will find a better solution... But you cat mark the thread as resolved if you are in hurry.
-
Sep 21st, 2010, 10:52 AM
#12
Re: LINQ datatable Update
I would ask, why does it have to be one line of code? Just because you can does not mean you should.
-
Sep 21st, 2010, 10:54 AM
#13
Thread Starter
Junior Member
Re: LINQ datatable Update
I thought that so the code is faster executed - but I've done some tests and I'm wrong..
-
Sep 21st, 2010, 10:59 AM
#14
Re: LINQ datatable Update
Originally Posted by tratak
I thought that so the code is faster executed - but I've done some tests and I'm wrong..
That (speed) is critical in regards to customer satisfaction rather than developer satisfaction
-
Sep 21st, 2010, 11:46 AM
#15
Re: LINQ datatable Update
So.... I'm confused... is this resolved or not?
-tg
-
Sep 21st, 2010, 11:47 AM
#16
Thread Starter
Junior Member
Re: LINQ datatable Update
Ok - let's say it's resolved !
Thank you !
-
Sep 21st, 2010, 06:18 PM
#17
Re: LINQ datatable Update
For future reference, please provide all the relevant information upfront in future. If you don't then we either have to make assumptions, i.e. guess, or else ask you to provide more information. The version of VB you're using is always relevant, which is why, when you create a new thread, there's a drop-down provided and a label right next to it that says "Please select your version".
Even that may not be enough sometimes though, because features differ not only between versions of VB but also versions of .NET, and some versions of VB can target multiple versions of .NET.
(I'm using LinqBridge with vs2008 with Framework 2.0)
That qualifies as relevant information. If we have to guess and we guess wrong (e.g. I assumed that you were using the most current version) then we have wasted our time and yours.
Finally, if the thread is resolved then please mark it so, using the Thread Tools menu. That way it shows up as resolved in the forum thread listing and people won't waste their time opening it to help when no more help is needed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|