Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Help with Linq in VB.net

Help with Linq in VB.net

Scheduled Pinned Locked Moved Visual Basic
csharpdatabaselinqhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chinners
    wrote on last edited by
    #1

    Hi, I know this may not be the place to post this, but it is about syntax in VB, so I thought it would be the best place. (P.s. This is a VERY simplified example of what I want to do... I wouldnt waste effort with linq on something this simple :) ) I have a list of structures in VB, and can easily select an item from the list:

    Public Structure UserInfo
    Public ID As Integer
    Public Name As String
    End Structure

    Dim Users As New List(Of UserInfo)

    Users.Add(New UserInfo With {.ID = 1, .Name="Jim"})
    Users.Add(New UserInfo With {.ID = 2, .Name="Bob"})
    Users.Add(New UserInfo With {.ID = 3, .Name="Sue"})

    Now, what I want to do is update user ID 3 (for example) to change the name from Sue to Susan... At the moment I use this:

      Dim User As UserInfo = (From UserInfo In Users Where UserInfo.ID = 3 Select UserInfo).First
      Users.Remove(User)
      User.Name = "Susan"
      Users.Add(User)
    

    This works, but surely there is a better way? I have tried rooting through Google, but I am not sure of what I should be looking for to filter out all the Linq to sql and c# examples :confused: Please help!

    G 1 Reply Last reply
    0
    • C Chinners

      Hi, I know this may not be the place to post this, but it is about syntax in VB, so I thought it would be the best place. (P.s. This is a VERY simplified example of what I want to do... I wouldnt waste effort with linq on something this simple :) ) I have a list of structures in VB, and can easily select an item from the list:

      Public Structure UserInfo
      Public ID As Integer
      Public Name As String
      End Structure

      Dim Users As New List(Of UserInfo)

      Users.Add(New UserInfo With {.ID = 1, .Name="Jim"})
      Users.Add(New UserInfo With {.ID = 2, .Name="Bob"})
      Users.Add(New UserInfo With {.ID = 3, .Name="Sue"})

      Now, what I want to do is update user ID 3 (for example) to change the name from Sue to Susan... At the moment I use this:

        Dim User As UserInfo = (From UserInfo In Users Where UserInfo.ID = 3 Select UserInfo).First
        Users.Remove(User)
        User.Name = "Susan"
        Users.Add(User)
      

      This works, but surely there is a better way? I have tried rooting through Google, but I am not sure of what I should be looking for to filter out all the Linq to sql and c# examples :confused: Please help!

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      A structure should generally be immutable, and the point of using a structure is mostly defated as you have a reference type inside it. Use a class instead of a structure.

      Public Class UserInfo
      Public ID As Integer
      Public Name As String
      End Structure

      Dim Users As New List(Of UserInfo)

      Users.Add(New UserInfo With {.ID = 1, .Name="Jim"})
      Users.Add(New UserInfo With {.ID = 2, .Name="Bob"})
      Users.Add(New UserInfo With {.ID = 3, .Name="Sue"})

      Now you don't have to remove and add the item to update it:

      (From UserInfo In Users Where UserInfo.ID = 3 Select UserInfo).First.Name = "Susan"

      Despite everything, the person most likely to be fooling you next is yourself.

      C 1 Reply Last reply
      0
      • G Guffa

        A structure should generally be immutable, and the point of using a structure is mostly defated as you have a reference type inside it. Use a class instead of a structure.

        Public Class UserInfo
        Public ID As Integer
        Public Name As String
        End Structure

        Dim Users As New List(Of UserInfo)

        Users.Add(New UserInfo With {.ID = 1, .Name="Jim"})
        Users.Add(New UserInfo With {.ID = 2, .Name="Bob"})
        Users.Add(New UserInfo With {.ID = 3, .Name="Sue"})

        Now you don't have to remove and add the item to update it:

        (From UserInfo In Users Where UserInfo.ID = 3 Select UserInfo).First.Name = "Susan"

        Despite everything, the person most likely to be fooling you next is yourself.

        C Offline
        C Offline
        Chinners
        wrote on last edited by
        #3

        Hi Guffa, Thanks for the reply - some very useful information in there. I do have one small problem though - I cant find the correct syntax for the update:

        (From UserInfo In Users Where UserInfo.ID = 3 Select UserInfo).First.Name = "Susan"

        It doesnt work on its own in VB 2k8, and after hours of net searching, I cant find the answer :((. Using:

        dim b as boolean = (From UserInfo In Users Where UserInfo.ID = 3 Select UserInfo).First.Name = "Susan"

        compiles, but obviously doesnt update anything (the expression above supposedly returns a boolean) If you get chance, and you know the answer, can you post a quick message telling me... Ta muchly!

        G 1 Reply Last reply
        0
        • C Chinners

          Hi Guffa, Thanks for the reply - some very useful information in there. I do have one small problem though - I cant find the correct syntax for the update:

          (From UserInfo In Users Where UserInfo.ID = 3 Select UserInfo).First.Name = "Susan"

          It doesnt work on its own in VB 2k8, and after hours of net searching, I cant find the answer :((. Using:

          dim b as boolean = (From UserInfo In Users Where UserInfo.ID = 3 Select UserInfo).First.Name = "Susan"

          compiles, but obviously doesnt update anything (the expression above supposedly returns a boolean) If you get chance, and you know the answer, can you post a quick message telling me... Ta muchly!

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          I thought that the LINQ expression could be used as an expression, but obviusly it can't. If you put the result in a variable, it works:

          Dim user As UserInfo = (From u In Users Where u.ID = 3 Select u).First
          user.Name = "Susan"

          Another way to do this (and more effective as it only looks until it finds the first occurance) is to use a lambda expression:

          Users.Find(Function(u) u.ID = 3).Name = "Susan"

          Despite everything, the person most likely to be fooling you next is yourself.

          C 1 Reply Last reply
          0
          • G Guffa

            I thought that the LINQ expression could be used as an expression, but obviusly it can't. If you put the result in a variable, it works:

            Dim user As UserInfo = (From u In Users Where u.ID = 3 Select u).First
            user.Name = "Susan"

            Another way to do this (and more effective as it only looks until it finds the first occurance) is to use a lambda expression:

            Users.Find(Function(u) u.ID = 3).Name = "Susan"

            Despite everything, the person most likely to be fooling you next is yourself.

            C Offline
            C Offline
            Chinners
            wrote on last edited by
            #5

            Hi Guffa, Thanks for that - you have been a huge help! I cant believe I didnt try option 1 first though :doh: I think I tried everything but!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups