如何将数据从实体框架发送到stimulsoft报告? [英] How to send data from entity framework to stimulsoft report?

查看:102
本文介绍了如何将数据从实体框架发送到stimulsoft报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hello。我已经编写了一个项目(窗口应用程序),它使用实体框架(代码优先)进行数据访问。

现在我想使用stimulsoft进行报告。但是来自stimulsoft的RegData方法,将数据表或数据集作为参数。

我不知道如何从实体框架向刺激报告发送数据?任何人都可以帮助我吗?

代码是:



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 :

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();

推荐答案

调用的结果:

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



IEnumerable< anonymous-type > 。没有内置的转换操作符将其转换为 DataSet



幸运的是,它并不太难自己写一个:


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:

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:

stiReport1.RegData(db.Books.Select(a => new { a.Title, a.Author }).ToDataSet());


这篇关于如何将数据从实体框架发送到stimulsoft报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆