Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8222679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:21:42+00:00 2026-06-07T14:21:42+00:00

I am new to LINQ, I want to convert LINQ query to DataTable DataClassesDataContext

  • 0

I am new to LINQ, I want to convert LINQ query to DataTable

    DataClassesDataContext db = new DataClassesDataContext(MyConncectionString);
    IEnumerable<DataRow> qry = (IEnumerable<DataRow>)(from tbl in db.Table1.AsEnumerable()
                                                     select tbl);
    DataTable dt=new DataTable();
    qry.CopyToDataTable(dt, LoadOption.OverwriteChanges);
    GridView1.DataSource = dt;

But I got error

Unable to cast object of type ‘WhereSelectEnumerableIterator to type ‘System.Collections.Generic.IEnumerable`1[System.Data.DataRow]’.

Can anyone please help me that

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:21:45+00:00Added an answer on June 7, 2026 at 2:21 pm

    These are basic concepts:

    There are several LINQ providers shipped with the .Net framework

    • LINQ to Objects. System.Linq. It’s the LINQ core and it is designed to work with in-memory collections

    • LINQ to XML. System.Xml.Linq To work with XML documents

    • LINQ to DataSet. To work with DataTable, and DataSet objects

    • LINQ to SQL. System.Data.Linq It is used to work with SQL Server databases

    • LINQ to Entities. (Entity Framework) Used to work with several databases. It provides a richer API

    What I think you are trying to do is:

    From a query returned from a LINQ to SQL context, you want to return a collection of DataRow objects.

    There’s no simple conversion between a LINQ to SQL object and a DataRow, so if you insist you would need to use reflection to get the desired output, Fastest way to fill DataTable from LINQ query using DataContext

    Now if you really want to work with the DataTable objects, then my recommendation is to use a strongly typed DataSet. To create one, simply add a new DataSet object to your project and from the Server Explorer drag tables like you would do with your LINQ to SQL objects

    If possible, evaluate the decision to use DataRow and instead return the objects generated by LINQ To SQL, or even better create your custom DTO objects

    About your code, I don’t see any reason to try to use a Datatable object (unless you are nto posting that extra code to justify this decision). If you just want to set the DataSource property of a control, simply do the following:

    var q = from tbl in db.Table1
            select tbl;
    
    this.myControl.DataSource = q;
    this.myControl.DataBind();
    

    If you need, you can transform the result into an anonymous object and used it as the DataSource:

    var q = from tbl in db.Table1
            select new
            {
               MyNewProperty = tbl.ExistingProperty,
               AnotherProperty = tbl.MyCoolerProperty
            };
    
    this.myControl.DataSource = q;
    this.myControl.DataBind();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use LINQ to convert this IEnumerable<int>[] value1ByType = new IEnumerable<int>[3]; value1ByType[0]=
I need to convert the linq query from generic ienumerable to arraylist. ArrayList myArrayList
how to extract DataTable or DataSet from Linq Query or List. e.g I have
I want to convert Following sql server query with Linq-to-sql query . What I
I want to convert below SQL query to linq query but I confused: SELECT
I am new to LINQ. playerData is a list<DataAccess.Team> and I want to initialize
I'm using LINQ-to-Entities. Using the following query: var x = from u in context.Users
Hy, I want to convert a sql command into linq, but I receive the
Is it possible to convert this code into one LINQ query. I could have
I have linq query as follows: var result = (from Customer cust in db

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.