How to get the value from database row wise using entity framework

Posted on 2012-08-23
Last Modified: 2012-08-27
     I am new in using ADO.NET entity data model. I would like to extract the column name and the corresponding row values from the sql server database for my program.
 I use the code below
   MyPgmEntities context = new MyPgmEntities();
         var customer= from c in context.tblcustomer select c;            
         Object LinqObject = new tblcustomer ();
           foreach (var  c in customer)
                foreach (PropertyInfo pi in LinqObject.GetType().GetProperties())
               //Get Column Name                          
               string sAtt = pi.Name;
               //Get row value for the corresponding column name 'sAtt'

Open in new window

Using the above code I am able to get the column name. But I would like to know how to loop through and get the corresponding row values in the database.

Question by:anjana81
    LVL 74

    Accepted Solution

    It should just be c.Name for the value.

    You typically (if not always) set up your entity to model the table(s) the data is coming from. This means if you have a table named Customer and that table has a column named Name, then in C# you have a class named Customer, with a property called Name.

    Author Comment

    As mentioned above if I give c.Name I can have the row value. But as I would like to loop through all the columns and get the column name and the corresponding row values. How can I achieve this ? for example something like c.sAtt  where sAtt is a string that  has the column name. This is because I have 40 columns and its better to get these row values in a loop rather than specifing c.Col1Name,c.col2Name,c.col3Name etc...


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now