LINQ datatable Update-VBForums
Results 1 to 17 of 17

Thread: LINQ datatable Update

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    20

    Question 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 !

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    89,080

    Re: LINQ datatable Update

    vb.net Code:
    1. Array.ForEach(dtable.AsEnumerable().ToArray(), Sub(row) row("MyField") = 77)

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster
    Join Date
    May 2002
    Posts
    25,961

    Re: LINQ datatable Update

    That works in 2010... for earlier versions, you'll need to replace "sub" with "function"

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    20

    Re: LINQ datatable Update

    Thanks !!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    20

    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)

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: LINQ datatable Update

    Quote Originally Posted by techgnome View Post
    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:
    1. Array.ForEach(dtable.AsEnumerable().ToArray(), AddressOf Me.ProcessRow)
    2.  
    3. Private Sub ProcessRow(ByVal row As DataRow)
    4.     row("MyField") = 77
    5. End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    20

    Re: LINQ datatable Update

    I found the solution - view the previous post !

    Thank you !

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    20

    Re: LINQ datatable Update

    Sorry - you have right ! An error is rising !

  9. #9
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: LINQ datatable Update

    Quote Originally Posted by tratak View Post
    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)
    Quote 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.

  10. #10
    PowerPoster
    Join Date
    May 2002
    Posts
    25,961

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    20

    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.

  12. #12
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    5,927

    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.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    20

    Re: LINQ datatable Update

    I thought that so the code is faster executed - but I've done some tests and I'm wrong..

  14. #14
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    5,927

    Re: LINQ datatable Update

    Quote Originally Posted by tratak View Post
    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

  15. #15
    PowerPoster
    Join Date
    May 2002
    Posts
    25,961

    Re: LINQ datatable Update

    So.... I'm confused... is this resolved or not?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    20

    Re: LINQ datatable Update

    Ok - let's say it's resolved !

    Thank you !

  17. #17
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    89,080

    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.