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. C#
  4. linq update problem

linq update problem

Scheduled Pinned Locked Moved C#
databasequestioncsharplinqhelp
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.
  • B Offline
    B Offline
    brain2cpu
    wrote on last edited by
    #1

    It works: var names = from n in db.Contacts where n.Name.Contains("aaa") select n; foreach(var t in names) { t.Name = "xxx"; } db.SubmitChanges(); Not works (no errors but the Name field does not change) public class tmp { public string Name {get; set;} public int ID { get; set; } public string Group { get; set; } } var names = from n in db.Contacts where n.Name.Contains("aaa") select new tmp { Name = n.Name, ID = n.Id, Group = n.Group.Name }; foreach(var t in names) { t.Name = "xxx"; } db.SubmitChanges(); how can I do this without fetching all database columns? Thanks

    T 1 Reply Last reply
    0
    • B brain2cpu

      It works: var names = from n in db.Contacts where n.Name.Contains("aaa") select n; foreach(var t in names) { t.Name = "xxx"; } db.SubmitChanges(); Not works (no errors but the Name field does not change) public class tmp { public string Name {get; set;} public int ID { get; set; } public string Group { get; set; } } var names = from n in db.Contacts where n.Name.Contains("aaa") select new tmp { Name = n.Name, ID = n.Id, Group = n.Group.Name }; foreach(var t in names) { t.Name = "xxx"; } db.SubmitChanges(); how can I do this without fetching all database columns? Thanks

      T Offline
      T Offline
      TJoe
      wrote on last edited by
      #2

      Sorry, if you are trying to update the Name on the contact object, then you will need to select the contact object (as you do with the first code example). In the second example you are creating a new object (with the same Name, Id and Group), and updating it's name. You are not updating the original contact object.

      Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

      B 1 Reply Last reply
      0
      • T TJoe

        Sorry, if you are trying to update the Name on the contact object, then you will need to select the contact object (as you do with the first code example). In the second example you are creating a new object (with the same Name, Id and Group), and updating it's name. You are not updating the original contact object.

        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

        B Offline
        B Offline
        brain2cpu
        wrote on last edited by
        #3

        I thought about this, but I did hope it's my mistake. So, there is no way to have an "updateable select" using just a few of table columns? The original goal was something like: dataViewGrid.DataSource = from ..... and permit updates, ... in grid having just 3-4 columns (not all 15).

        T 1 Reply Last reply
        0
        • B brain2cpu

          I thought about this, but I did hope it's my mistake. So, there is no way to have an "updateable select" using just a few of table columns? The original goal was something like: dataViewGrid.DataSource = from ..... and permit updates, ... in grid having just 3-4 columns (not all 15).

          T Offline
          T Offline
          TJoe
          wrote on last edited by
          #4

          Yes it should be possible to only retrieve 3-4 columns from a DB table and still allow those columns to be updated. This is assuming ofcourse that one of those columns is a unique identifier for the associated row (e.g. the Primary Key). For your code, it really depends on what db.Contacts returns. Since I can't see that code, I'm assuming that it returns a list of Contact objects that have all 15 columns. In order to only return 3-4 columns, then the Contact object would need to only have the 3-4 columns. If you can provide some more of the code that would help (either through email or here). Let me know if this doesn't make sense also. But basically, the "object" that holds the data for a single row in your table is what must be updated. Therefore, you would need to change how many columns the "object" has before the code you posted earlier.

          Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

          B 1 Reply Last reply
          0
          • T TJoe

            Yes it should be possible to only retrieve 3-4 columns from a DB table and still allow those columns to be updated. This is assuming ofcourse that one of those columns is a unique identifier for the associated row (e.g. the Primary Key). For your code, it really depends on what db.Contacts returns. Since I can't see that code, I'm assuming that it returns a list of Contact objects that have all 15 columns. In order to only return 3-4 columns, then the Contact object would need to only have the 3-4 columns. If you can provide some more of the code that would help (either through email or here). Let me know if this doesn't make sense also. But basically, the "object" that holds the data for a single row in your table is what must be updated. Therefore, you would need to change how many columns the "object" has before the code you posted earlier.

            Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

            B Offline
            B Offline
            brain2cpu
            wrote on last edited by
            #5

            db.Contacts and 3 more classes are generated by sqlmetal, the solution would be to modify this classes to have properties just for database columns I need? (the primary key is in my "column-wish-list"). This probably would work but it seems to me way to complicated and across the program there are about 5 column combinations. Thanks

            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