locked
How to get data and columns from linq dynamically RRS feed

  • Question

  • Hi,

             I have a problem with linq in my scenario, where table name comes dynamically and i need to fetch all columns and records from that table. for example..

    public void GetData(string tableName,int id)
            {
                DataClasses1DataContext _db = new DataClasses1DataContext();
                var qry = from o in _db.Organizations where o.OrganizationId==id select o;
                //but here, I cannot pass Organizations table directly as It is dynamic
                        
            }

    So how can I give table name dynamically in context, and how to get columns also for that table.

    Please give me some idea.

    Thanks

    VD

    <style type="text/css">img.imageResizerActiveClass{cursor:nw-resize !important;outline:1px dashed black !important;} img.imageResizerChangedClass{z-index:300 !important;max-width:none !important;max-height:none !important;} img.imageResizerBoxClass{margin:auto; z-index:99999 !important; ; top:0; left:0; right:0; bottom:0; border:1px solid white; outline:1px solid black;} </style>
    Monday, September 2, 2013 7:12 AM

Answers

  • In programming language / object oriented programming with strongly typed data types it's not possible to act in this dynamically way.

    You can use ".NET reflection" to access properties by there names, but not for Linq / data context.


    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Monday, September 2, 2013 11:16 AM

All replies

  • Then use a switch case statement: When tableName == "a" then query the one context, when tableName == "b" then query an other context and so on.


    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Monday, September 2, 2013 7:37 AM
  • No I cannot use switch case statement, becuase table name comes dynamically and it is not fixed, tables would be increased, and in that case, I have to change again in my switch case that is not possible for me.

    So basically I am looking for some solution which can access data and columns both from given table name through linq.

    <style type="text/css">img.imageResizerActiveClass{cursor:nw-resize !important;outline:1px dashed black !important;} img.imageResizerChangedClass{z-index:300 !important;max-width:none !important;max-height:none !important;} img.imageResizerBoxClass{margin:auto; z-index:99999 !important; ; top:0; left:0; right:0; bottom:0; border:1px solid white; outline:1px solid black;} </style>


    <style type="text/css">img.imageResizerActiveClass{cursor:nw-resize !important;outline:1px dashed black !important;} img.imageResizerChangedClass{z-index:300 !important;max-width:none !important;max-height:none !important;} img.imageResizerBoxClass{margin:auto; z-index:99999 !important; ; top:0; left:0; right:0; bottom:0; border:1px solid white; outline:1px solid black;} </style>
    • Edited by VD Tripathi Monday, September 2, 2013 11:02 AM
    Monday, September 2, 2013 11:02 AM
  • In programming language / object oriented programming with strongly typed data types it's not possible to act in this dynamically way.

    You can use ".NET reflection" to access properties by there names, but not for Linq / data context.


    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Monday, September 2, 2013 11:16 AM