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
  1. Home
  2. General Programming
  3. LINQ
  4. linq tablename in variable

linq tablename in variable

Scheduled Pinned Locked Moved LINQ
csharplinqhelptutorial
8 Posts 5 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.
  • H Offline
    H Offline
    haseeb_saeed
    wrote on last edited by
    #1

    Hi i am making a function that should return id for a column(the column is unique) , so there would be one key per that column. i want to make a function and do some thing like this with linq. please provide help on how to do so void int GetPrimaryKey(string tablename,string columnName,columnvalue) { mydatacontext d=new mydatacontext(); id=//select key from tablename where columnName=columnvalue// } please provid help... thanx

    haseeb

    S A 2 Replies Last reply
    0
    • H haseeb_saeed

      Hi i am making a function that should return id for a column(the column is unique) , so there would be one key per that column. i want to make a function and do some thing like this with linq. please provide help on how to do so void int GetPrimaryKey(string tablename,string columnName,columnvalue) { mydatacontext d=new mydatacontext(); id=//select key from tablename where columnName=columnvalue// } please provid help... thanx

      haseeb

      S Offline
      S Offline
      SayreCC
      wrote on last edited by
      #2

      Hi, you can try this var q = from a in d.gettable(); where a.id == textbox1.text;

      1 Reply Last reply
      0
      • H haseeb_saeed

        Hi i am making a function that should return id for a column(the column is unique) , so there would be one key per that column. i want to make a function and do some thing like this with linq. please provide help on how to do so void int GetPrimaryKey(string tablename,string columnName,columnvalue) { mydatacontext d=new mydatacontext(); id=//select key from tablename where columnName=columnvalue// } please provid help... thanx

        haseeb

        A Offline
        A Offline
        Andreas X
        wrote on last edited by
        #3

        i would use lambda. like this! id = d.GetTable().Where(row => row.columnName=columnvalue).Select(row => row.ID);

        Andreas Johansson
        IT Professional at Office IT Partner i Norrbotten Sweden
        What we don't know. We learn.
        What you don't know. We teach

        R 1 Reply Last reply
        0
        • A Andreas X

          i would use lambda. like this! id = d.GetTable().Where(row => row.columnName=columnvalue).Select(row => row.ID);

          Andreas Johansson
          IT Professional at Office IT Partner i Norrbotten Sweden
          What we don't know. We learn.
          What you don't know. We teach

          R Offline
          R Offline
          roopam_vishwam
          wrote on last edited by
          #4

          Hi , I'm looking for the same solution ; but i can't get it works. Please see my sample scenario below : public void RegularCustomers() { NorthwindDataContext db= new NorthwindDataContext(); string tableName = db.Customers; int age = 25; var LastNames = GetLastName ( tableName , age); } public string GetLastName(string tableName , int age) { NorthwindDataContext db= new NorthwindDataContext(); var names =( from client in db.GetTable(tableName) where client.age >= age select client.LastName ).ToList(); return names; } Please can you provide the solution for above scenario . Regards, Chary

          chary

          A 1 Reply Last reply
          0
          • R roopam_vishwam

            Hi , I'm looking for the same solution ; but i can't get it works. Please see my sample scenario below : public void RegularCustomers() { NorthwindDataContext db= new NorthwindDataContext(); string tableName = db.Customers; int age = 25; var LastNames = GetLastName ( tableName , age); } public string GetLastName(string tableName , int age) { NorthwindDataContext db= new NorthwindDataContext(); var names =( from client in db.GetTable(tableName) where client.age >= age select client.LastName ).ToList(); return names; } Please can you provide the solution for above scenario . Regards, Chary

            chary

            A Offline
            A Offline
            Andreas X
            wrote on last edited by
            #5

            Hi! What is not working? What is the error message?

            Andreas Johansson
            IT Professional at Office IT Partner i Norrbotten Sweden
            What we don't know. We learn.
            What you don't know. We teach

            R 1 Reply Last reply
            0
            • A Andreas X

              Hi! What is not working? What is the error message?

              Andreas Johansson
              IT Professional at Office IT Partner i Norrbotten Sweden
              What we don't know. We learn.
              What you don't know. We teach

              R Offline
              R Offline
              roopam_vishwam
              wrote on last edited by
              #6

              The above is just a scenario ; what i need is a working code of using table name as variable using linq-sql. Thanks in advance.

              A 1 Reply Last reply
              0
              • R roopam_vishwam

                The above is just a scenario ; what i need is a working code of using table name as variable using linq-sql. Thanks in advance.

                A Offline
                A Offline
                Andreas X
                wrote on last edited by
                #7

                Hi! Instead of using string tableName, try using Type tableType.

                Andreas Johansson
                IT Professional at Office IT Partner i Norrbotten Sweden
                What we don't know. We learn.
                What you don't know. We teach

                Z 1 Reply Last reply
                0
                • A Andreas X

                  Hi! Instead of using string tableName, try using Type tableType.

                  Andreas Johansson
                  IT Professional at Office IT Partner i Norrbotten Sweden
                  What we don't know. We learn.
                  What you don't know. We teach

                  Z Offline
                  Z Offline
                  Zev
                  wrote on last edited by
                  #8

                  I still don't understand the answers. The question was very clear and simple. We have a SQL select statement in which the table name is variable. For example: "Select Foo From " + strTableName; Where strTableName is a variable holding the table name. now, can anyone give me the linq lingo of this simple statment? and please don't tell me to use TableType, I have no idea what's that got to do with the strTableName. And if you do say, use TableType, please use the same simple example to demonstrate how, given the vaiable strTableName, can we obtain the table type. 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