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 query to DataTable

Linq query to DataTable

Scheduled Pinned Locked Moved LINQ
questioncsharpdatabaselinq
4 Posts 3 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.
  • U Offline
    U Offline
    u2envy12
    wrote on last edited by
    #1

    How do I convert a Linq Query to a DataTable. Code Follows ? using (EmployeeDataContext empCtx = new EmployeeDataContext(GlobalMethods.SqlConnectionString)) { var rsEmp = from emp in empCtx.Employees join payp in empCtx.PayPeriods on emp.PayperiodID equals payp.PayPeriodID join rst in empCtx.Rosters on emp.RosterID equals rst.RosterID join dep in empCtx.Departments on emp.DepartmentID equals dep.DepartmentID join div in empCtx.Divisions on emp.DivisionID equals div.DivisionID join cst in empCtx.CostCentres on emp.CostCentreID equals cst.CostCentreID join grp in empCtx.Groups on emp.GroupID equals grp.GroupID where emp.OrganizationID == UserClass.OrganizationID orderby emp.EmployeeNumber select new { emp.EmployeeID, emp.EmployeeNumber, emp.EmployeeName, emp.Surname, emp.BadgeNumber, payp.PayPeriodName, rst.RosterName, div.DivisionName, dep.DepartmentName, cst.CostCentreName, grp.GroupName, emp.EmploymentStatus, emp.EndOfEmployment }; DataTable myTable = (DataTable)rsEmp; DataView filteredData = new DataView(myTable); dataGridView1.DataSource = filteredData; //Or //OR DataTable myTable = rsEmp.CopyToDataTable(); dataGridView1.DataSource = myTable ; }

    K 1 Reply Last reply
    0
    • U u2envy12

      How do I convert a Linq Query to a DataTable. Code Follows ? using (EmployeeDataContext empCtx = new EmployeeDataContext(GlobalMethods.SqlConnectionString)) { var rsEmp = from emp in empCtx.Employees join payp in empCtx.PayPeriods on emp.PayperiodID equals payp.PayPeriodID join rst in empCtx.Rosters on emp.RosterID equals rst.RosterID join dep in empCtx.Departments on emp.DepartmentID equals dep.DepartmentID join div in empCtx.Divisions on emp.DivisionID equals div.DivisionID join cst in empCtx.CostCentres on emp.CostCentreID equals cst.CostCentreID join grp in empCtx.Groups on emp.GroupID equals grp.GroupID where emp.OrganizationID == UserClass.OrganizationID orderby emp.EmployeeNumber select new { emp.EmployeeID, emp.EmployeeNumber, emp.EmployeeName, emp.Surname, emp.BadgeNumber, payp.PayPeriodName, rst.RosterName, div.DivisionName, dep.DepartmentName, cst.CostCentreName, grp.GroupName, emp.EmploymentStatus, emp.EndOfEmployment }; DataTable myTable = (DataTable)rsEmp; DataView filteredData = new DataView(myTable); dataGridView1.DataSource = filteredData; //Or //OR DataTable myTable = rsEmp.CopyToDataTable(); dataGridView1.DataSource = myTable ; }

      K Offline
      K Offline
      K V Sekhar
      wrote on last edited by
      #2

      Hi, Check this link....

      U 1 Reply Last reply
      0
      • K K V Sekhar

        Hi, Check this link....

        U Offline
        U Offline
        u2envy12
        wrote on last edited by
        #3

        Have you tried this method ? I get an error on PropertyInfo[] oProps = null; Where is this created ?

        A 1 Reply Last reply
        0
        • U u2envy12

          Have you tried this method ? I get an error on PropertyInfo[] oProps = null; Where is this created ?

          A Offline
          A Offline
          ABitSmart
          wrote on last edited by
          #4

          System.Reflection.PropertyInfo ?? Try the second method, it looks cleaner than the reflection one. You might have to take a look at the GetCommand[^] method. I presume the samples posted work ?

          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