Saturday, January 5, 2013

Get DataTable from LINQ query


Using the EntityDataReader class we can retrieve a DataTable froma LINQ query. Below is a simple select query example.

public DataTable GetContact()

        {
            AirTouchDataContext airTouch; // LINQ DataContext
            DT = new DataTable();
            airTouch = new AirTouchDataContext(DBConnectionString);
            DT = (from tblcon in airTouch.tblContacts
                  select new
                  {
                      tblcon.ContactID,
                      tblcon.Address,
                      tblcon.Name,
                      tblcon.Contact,
                      tblcon.Designation,
                      tblcon.AddedBy,
                      tblcon.Status,
                      tblcon.TimeStamp

                  }).ToDataTable();
            return DT;
        }

You need to add the "using Microsoft.Samples.EntityDataReader" as reference.

No comments:
Write comments
Recommended Posts × +