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

  • Home
  • SEARCH
  • 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 701083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:34:12+00:00 2026-05-14T03:34:12+00:00

I am trying to perform a LINQ query on a DataTable and show the

  • 0

I am trying to perform a LINQ query on a DataTable and show the result in another DataTable. My source DataTable looks something like this:

DataTable myDataTable = new DataTable();
myDataTable.Columns.Add("OrderID", typeof(int));
myDataTable.Columns.Add("Date", typeof(DateTime));
myDataTable.Columns.Add("UnitsPurchased", typeof(int));

The resulting DataTable looks like this when filled:

Order ID   Date    Units Purchased  
16548    10/15/09      250  
17984    11/03/09      512   
20349    01/11/10      213  
34872    01/15/10      175

My current LINQ query looks like this:

IEnumerable<DataRow> query = (from row in myDataTable.AsEnumerable()
                              where row.UnitsPurchased > 200
                              select new
                              {
                                 row.OrderID,
                                 row.Date,
                                 row.UnitsPurchased
                              }) as IEnumerable<DataRow>;

resultDataTable.DataSource = query.CopyToDataTable<DataRow>();

Every time I run this code query is null. I can see that that the as IEnumerable<DataRow> is the culprit, but it makes no since to me since DataTable.AsEnumerable() returns an IEnumerable<DataRow>. Any help would be appreciated.

  • 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-05-14T03:34:12+00:00Added an answer on May 14, 2026 at 3:34 am

    When you select new { }, you’re actually getting an IEnumerable<(Anonymous Type)>, not IEnumerable<DataRow>. So your as IEnumerable<DataRow> will return null, since it can’t be directly cast.

    Either select new MyDataRow(constructor using values...) or something, or just do var query =... without the as cast. There’s an msdn article about using CopyToDataTable with a non-DataRow generic parameter, though I haven’t read it in depth, but selecting new DataRows is probably the easier solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to perform a LINQ query on a DataTable object and bizarrely I
I'm trying to perform a simple Linq find first query on a typed DataTable,
I'm trying to perform a simple LINQ query on the Columns property of a
I'm trying to perform a linq to entities query on a table that's inherited
I am trying to perform the following linq to entities query- var data =
I am trying to perform jQuery each function on something like: RelatedDoc: [ {
I'm trying to perform a SQL query through a linked SSAS server. The initial
I'm trying to perform a bitwise NOT in SQL Server. I'd like to do
In this situation I am trying to perform a data import from an XML
I need to perform a LINQ query for both a time period from a

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.