Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello.I have writed a project(window application) that use entity framework(code first) for data accessing.
Now I want use stimulsoft for reporting . but RegData method from stimulsoft , get datatable or dataset as argumant.
and I don't Know that how send data from entity framework to stimul report ? anybody can help me?
code for that is :

C#
DatabaseContext db = new DatabaseContext();// this is my context
dataGridView1.DataSource = db.Books.ToList();
stiReport1.RegData((DataSet)db.Books.Select(a => new { a.Title,a.Author}));//this line encounter to error
stiReport1.Compile();
stiReport1.Design();
stiReport1.Render();
stiReport1.Show();
Posted
Updated 4-Mar-15 4:30am
v2

1 solution

The result of calling:
C#
db.Books.Select(a => new { a.Title,a.Author})

is an IEnumerable<anonymous-type>. There is no built-in conversion operator to turn that into a DataSet.

Fortunately, it's not too hard to write one yourself:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

namespace YourNamespace
{
    public static class Extensions
    {
        public static DataTable ToDataTable<T>(this IEnumerable<T> source, string tableName = null)
        {
            if (source == null) throw new ArgumentNullException("source");
            
            var properties = TypeDescriptor.GetProperties(typeof(T))
                .Cast<PropertyDescriptor>()
                .ToList();
            
            var result = new DataTable(tableName);
            result.BeginInit();

            foreach (var prop in properties)
            {
                result.Columns.Add(prop.Name, prop.PropertyType);
            }

            result.EndInit();
            result.BeginLoadData();
            
            foreach (T item in source)
            {
                object[] values = properties.Select(p => p.GetValue(item)).ToArray();
                result.Rows.Add(values);
            }
            
            result.EndLoadData();
            
            return result;
        }

        public static DataSet ToDataSet<T>(this IEnumerable<T> source, string dataSetName = null, string tableName = null)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (string.IsNullOrEmpty(dataSetName)) dataSetName = "NewDataSet";

            var result = new DataSet(dataSetName);
            result.Tables.Add(source.ToDataTable(tableName));
            return result;
        }
    }
}


With that extension method in place, you should then be able to call:
C#
stiReport1.RegData(db.Books.Select(a => new { a.Title, a.Author }).ToDataSet());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900