水晶报表和 LINQ

发布于 2024-07-06 04:10:01 字数 51 浏览 5 评论 0原文

有人知道如何将 Crystal Reports 与 Linq to SQL 结合使用吗?

Has anyone figured out how to use Crystal Reports with Linq to SQL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟─花易冷 2024-07-13 04:10:02

如果您有 dbnull 值,上面的代码将无法在 Web 应用程序中运行。 您必须将结果列表对象转换为数据集或数据表。 没有内置的方法。 我也遇到过同样的问题,经过几个小时在互联网上的探索,我找到了解决方案,并想在这里分享以帮助任何遇到问题的人。 你必须在你的项目中创建一个类:-

 public class CollectionHelper
    {
        public CollectionHelper()
        {
        }

        // this is the method I have been using
        public DataTable ConvertTo<T>(IList<T> list)
        {
            DataTable table = CreateTable<T>();
            Type entityType = typeof(T);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (T item in list)
            {
                DataRow row = table.NewRow();

                foreach (PropertyDescriptor prop in properties)
                {
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                }

                table.Rows.Add(row);
            }

            return table;
        }

        public static DataTable CreateTable<T>()
        {
            Type entityType = typeof(T);
            DataTable table = new DataTable(entityType.Name);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (PropertyDescriptor prop in properties)
            {
                // HERE IS WHERE THE ERROR IS THROWN FOR NULLABLE TYPES
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(
            prop.PropertyType) ?? prop.PropertyType);
            }

            return table;
        }
    }

在这里设置你的水晶报告

CrystalReport1 cr1 = new CrystalReport1();

            var results = (from obj in context.tSamples
                           where obj.ID == 112
                           select new { obj.Name, obj.Model, obj.Producer }).ToList();
            CollectionHelper ch = new CollectionHelper();
            DataTable dt = ch.ConvertTo(results);
            cr1.SetDataSource(dt);
            crystalReportsViewer1.ReportSource = cr1;

The above code wont work in web application if you have dbnull values. You have to convert the results list object to dataset or datatable. There is no built in method for it. I have gone through the same issue and after hours of exploring on the internet, I found the solution and wanna share here to help anyone stuck up with it. You have to make a class in your project:-

 public class CollectionHelper
    {
        public CollectionHelper()
        {
        }

        // this is the method I have been using
        public DataTable ConvertTo<T>(IList<T> list)
        {
            DataTable table = CreateTable<T>();
            Type entityType = typeof(T);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (T item in list)
            {
                DataRow row = table.NewRow();

                foreach (PropertyDescriptor prop in properties)
                {
                    row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                }

                table.Rows.Add(row);
            }

            return table;
        }

        public static DataTable CreateTable<T>()
        {
            Type entityType = typeof(T);
            DataTable table = new DataTable(entityType.Name);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (PropertyDescriptor prop in properties)
            {
                // HERE IS WHERE THE ERROR IS THROWN FOR NULLABLE TYPES
                table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(
            prop.PropertyType) ?? prop.PropertyType);
            }

            return table;
        }
    }

and here setting up your crystal report

CrystalReport1 cr1 = new CrystalReport1();

            var results = (from obj in context.tSamples
                           where obj.ID == 112
                           select new { obj.Name, obj.Model, obj.Producer }).ToList();
            CollectionHelper ch = new CollectionHelper();
            DataTable dt = ch.ConvertTo(results);
            cr1.SetDataSource(dt);
            crystalReportsViewer1.ReportSource = cr1;
何时共饮酒 2024-07-13 04:10:02

尽管我自己没有尝试过,但似乎可以通过使用 DataContext.LoadOptions 的组合来使其渴望接受关系和 GetCommand(IQueryable) 返回保留关系的 SQLCommand 对象。

有关详细信息,请参阅 MSDN 论坛

Altough I haven't tried it myself it seems to be possible by using a combination of DataContext.LoadOptions to make it eager to accept relations and GetCommand(IQueryable) to return a SQLCommand object that preserves relations.

See more info on MSDN Forums.

ゃ懵逼小萝莉 2024-07-13 04:10:01

您可以将 LINQ 结果集转换为 List,您不需要严格使用 DataSet 作为报表 SetDataSource,您可以提供 Crystal Reports带有 IEnumerable 的数据。 由于 List 继承自 IEnumerable,因此您可以将报表的数据源设置为列表,只需调用 .ToList() 方法即可您的 LINQ 结果集。 基本上:

        CrystalReport1 cr1 = new CrystalReport1();

        var results = (from obj in context.tSamples
                      where obj.ID == 112
                      select new { obj.Name, obj.Model, obj.Producer }).ToList();

        cr1.SetDataSource(results);
        crystalReportsViewer1.ReportSource = cr1;

You can convert your LINQ result set to a List, you need not strictly use a DataSet as the reports SetDataSource, you can supply a Crystal Reports data with an IEnumerable. Since List inherits from IEnumerable you can set your reports' Data Source to a List, you just have to call the .ToList() method on your LINQ result set. Basically:

        CrystalReport1 cr1 = new CrystalReport1();

        var results = (from obj in context.tSamples
                      where obj.ID == 112
                      select new { obj.Name, obj.Model, obj.Producer }).ToList();

        cr1.SetDataSource(results);
        crystalReportsViewer1.ReportSource = cr1;
五里雾 2024-07-13 04:10:01

msdn 文档建议您可以将 Crystal Report 绑定到 ICollection。

我可以推荐一个 List(T) 吗?

The msdn doc's suggest that you can bind a Crystal Report to an ICollection.

Might I recommend a List(T) ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文