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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:06:04+00:00 2026-05-21T22:06:04+00:00

I’m migrating an older .net application to .net 4, this migration has to be

  • 0

I’m migrating an older .net application to .net 4, this migration has to be done in several stages, thats why some of the methods might seem a bit unconventional. Anyway…

What I have is a Stored Procedure (Analysis_select) returning one row with several columns with the result. If i call it with

var result = dbContext.Analysis_select(user.UserId, Year, Week);

everything is fine, i can view the data in with the debugger or display it in a grid view or something like that, so the expression and Stored Procedure really works! But the result is not compatible with the rest of the code so…

If I try to cast it to DataSet it fails, Visual Studio actually sais this is ok but when rendering on a web page it crashes

var result = (DataSet)dbContext.Analysis_select(user.UserId, Year, Week);

The error is as follows

Unable to cast object of type ‘SingleResult`1[Analysis_select]’ to type ‘System.Data.DataSet’.

I’ve read about some other conversions from linq to DataSet but most of the methods seems a bit excessive for this. The reason why I want to keep the DataSet is that there’s tens of thousands of lines of code depending on such results. Sucks yes, but can you help me fix this?

Any help is highly appreciated, thanks!

  • 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-21T22:06:04+00:00Added an answer on May 21, 2026 at 10:06 pm

    You will need to write an extension method to convert the IEnumerable into a DataSet. Here is an example of how to convert IEnumerable to a DataTable.

    private DataTable ToDataTable<T>(List<T> items)
    {
        var table = new DataTable(typeof (T).Name);
    
        PropertyInfo[] props = typeof (T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    
        foreach (PropertyInfo prop in props)
        {
            Type t = GetCoreType(prop.PropertyType);
            table.Columns.Add(prop.Name, t);
        }
    
        foreach (T item in items)
        {
            var values = new object[props.Length];
    
            for (int i = 0; i < props.Length; i++)
            {
                values[i] = props[i].GetValue(item, null);
            }
    
            table.Rows.Add(values);
        }
    
        return table;
    }
    public static Type GetCoreType(Type t)
    {
        if (t != null && IsNullable(t))
        {
            if (!t.IsValueType)
            {
                return t;
            }
            else
            {
                return Nullable.GetUnderlyingType(t);
            }
        }
        else
        {
            return t;
        }
    }
    public static bool IsNullable(Type t)
    {
        return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
    }
    

    Here’s a link to the source of this solution: http://www.chinhdo.com/20090402/convert-list-to-datatable/

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

Sidebar

Related Questions

No related questions found

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.