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 3939530
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:18:38+00:00 2026-05-20T00:18:38+00:00

I am trying to run a linq query but I need the result as

  • 0

I am trying to run a linq query but I need the result as a datatable as I am using that to store records from different queries in the same viewstate object.

The 2 versions below compile, but return an empty set. The exact error is “Value cannot be null.
Parameter name: source”
. (and yes I have checked there is data):

MyDatabaseDataContext db = new MyDatabaseDataContext(conn); 
IEnumerable<DataRow> queryProjects = 
    (from DataRow p in db.STREAM_PROJECTs.AsEnumerable()
    where p.Field<int>("STREAM_ID") == StreamID
    select new
    {
        PROJECT_ID = p.Field<int>("PROJECT_ID"),
        PROJECT_NAME = p.Field<string>("PROJECT_NAME")
    }) as IEnumerable<DataRow>;
DataTable results = queryProjects.CopyToDataTable<DataRow>();

…

//(from p in db.STREAM_PROJECTs.AsEnumerable()
//where p.STREAM_ID == StreamID
//select new
//{
//    p.PROJECT_NAME,
//    p.PROJECT_ID
//}) as IEnumerable<DataRow>;

The examples in this thread don’t seem to work in this situation either.

I guess I could just run a sql query command the old-fashioned way, but isn’t linq supposed to be quicker?

  • 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-20T00:18:39+00:00Added an answer on May 20, 2026 at 12:18 am

    Your problem is this:

    as IEnumerable<DataRow>
    

    The as keyword performs a safe cast, not a conversion, which it seems like you might think that it’s doing. The as keyword is semantically the same as doing this:

    IEnumerable<DataRow> queryProjects = 
        (IEnumerable<DataRow>)(from DataRow p in db.STREAM_PROJECTs.AsEnumerable()
        where p.Field<int>("STREAM_ID") == StreamID
        select new
        {
            PROJECT_ID = p.Field<int>("PROJECT_ID"),
            PROJECT_NAME = p.Field<int>("PROJECT_NAME")
        });
    

    Except the version with as won’t throw an exception when it fails to cast your query object (which is an IQueryable<T>, where T is an anonymous type) to an IEnumerable<DataRow> (which it isn’t).

    Unfortunately, there is no built-in method that I’m aware of that will take an enumerable of a concrete type (like your anonymous type in this example) and turn it into a DataTable. Writing one wouldn’t be too complicated, as you’d essentially need to get the properties reflectively then iterate over the collection and use those properties to create columns in a DataTable. I’ll post an example in a few.

    Something like this, placed in a static class within a namespace that you’re using, should provide an extension method that will do what you want:

    public static DataTable ToDataTable<T>(this IEnumerable<T> source)
    {
        PropertyInfo[] properties = typeof(T).GetProperties();
    
        DataTable output = new DataTable();
    
        foreach(var prop in properties)
        {
            output.Columns.Add(prop.Name, prop.PropertyType);
        }
    
        foreach(var item in source)
        {
            DataRow row = output.NewRow();
    
            foreach(var prop in properties)
            {
                row[prop.Name] = prop.GetValue(item, null);
            }
    
            output.Rows.Add(row);
        }
    
        return output;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to run a Linq query on a datatable to produce a result,
I'm trying to run a LINQ to SQL query that returns a result in
I am trying to run a Linq-to-SQL query, but when the query is evaluated,
I am trying to run a linq query using EF on .NET 3.5 and
Trying to write dynamic queries using the LINQ provider for NHibernate, but I am
I'm trying to bind a gridview to a linq to sql query that's using
I am trying to use a dynamic linq query to retrieve an IEnumerable<T> from
I'm trying to rewrite a SQL query in LINQ to Entities. I'm using LINQPad
Greetings, I'm trying to write a Linq query to run on a list of
I em using Linq to EF and trying to get FirstOrDefault entitiy from ObjectSet.

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.