如何用linq查询创建Crystal Report [英] How to create Crystal Report with query by linq

查看:64
本文介绍了如何用linq查询创建Crystal Report的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我在使用查询linq的结果创建晶体报告时遇到问题

在这里,我查询表视图中的所有字段:

hello everyone, I have a problem when use result from query linq to create crytal report

In here, I query all field in my table view:

CrystalReport2 _crpt2 = new CrystalReport2();
           var result = (from p in DB.vReportScoreSheetStudents
                         select new
                         {
                             StudentID = p.StudentID,
                             LastName = p.LastName,
                             FirstName = p.FirstName,
                             DateOfBirth = p.DateOfBirth == null ? DateTime.Now : p.DateOfBirth,
                             Address = p.Address == null ? "" : p.Address,
                             Class = p.Class == null ? "" : p.Class,
                             CourseID= p.CourseID,
                             SubjectName = p.SubjectName == null ? "" : p.SubjectName,
                             Year = p.Year == null ? DateTime.Now.Year : p.Year,
                             TermNo = p.TermNo == null ? 0 : p.TermNo,
                             Assignment= p.Assignment==null?0.0:p.Assignment,
                             Midterm = p.Midterm==null?0.0:p.Midterm,
                             Practice=p.Practice==null?0.0:p.Practice,
                             Project=p.Project==null?0.0:p.Project,
                             Endterm=p.Endterm==null?0.0:p.Endterm,
                             FinalMatch=p.FinalMatch==null?0.0:p.FinalMatch,
                             Note= p.Note==null?"":p.Note
                         }).ToList();
           if (result == null)
           {
               MessageBox.Show("null");
           }
           else
           {
               _crpt2.Load(@"CrystalReport2.rpt");
               _crpt2.SetDataSource(result);
               crystalReportViewer1.ReportSource = _crpt2;
           }


我所有的字段都有值,但是我仍然收到异常:


althuogh all my fields have value which.but I still receive a exception :

DataSet does not support System.Nullable<>.

from

from

_crpt2.SetDataSource(result);



当我临时删除某个字段时,它就可以了:



when I temporarily removed some field then its ok:

ReportDocument _rpt1 = new CrystalReport1();
          var data = (from p in DB.vReportScoreSheetStudents
                      select new
                      {
                          StudentID = p.StudentID,
                          p.LastName,
                          p.FirstName,
                          p.CourseID,
                          p.SubjectName,
                          Address = p.Address == null ? "" : p.Address,
                      }).ToList();
          _rpt1.Load(@"CrystalReport1.rpt");
          _rpt1.SetDataSource(data);
          crystalReportViewer1.ReportSource = _rpt1;

推荐答案

这就是它:您不能使用 nullable 类型.您没有提供p类型的声明,但是在您的代码中有一些它的成员,我猜他们的声明是这样的:
This is what it is: you cannot use nullable types. You did not provide declaration of the type of p but by some its members in your code I can guess their declarations are like this:
int? Year;
int? TermNo;
//maybe uint? ulong? or something like this



否则与我在您的代码中看到的null进行比较将无法编译.

您可以删除成员类型中的?",并且您的代码应该可以工作.如果由于需要使用代码中未声明的p类型而无法执行此操作,则应将类型转换为不可为null的类型.例如,涉及成员YearTermNo的行应这样修改:



Otherwise comparison with null I can see in your code would not compile.

You can get rid of ''?'' in member types and your code should work. If you cannot do it because you need to use the type of p which is not declared in your code, you should convert the types to non-nullable. For example, the lines involving the members Year and TermNo should be modified like this:

//...
Year = p.Year == null ? DateTime.Now.Year : p.Year.Value,
TermNo = p.TermNo == null ? 0 : p.TermNo.Value,
//...





另外,请记住:如果要在ADO.NET中使用数据库NULL,则需要使用System.DBNull类,string.Empty;通常,请避免使用立即常量,尤其要避免使用字符串立即常量,而是声明并使用显式常量,使用资源或从数据文件中读取的数据.

—SA





Also, remember: if you want to use database NULL in ADO.NET, you need to use System.DBNull class, http://msdn.microsoft.com/en-us/library/system.dbnull.aspx[^].

Probably, this was a root cause of the exception you had.
[END EDIT]

A bonus advice: don''t use "", use string.Empty instead; in general, avoid immediate constants, especially string immediate constants by all means, instead, declare and use explicit constants, use resources or data read from data files.

—SA


这篇关于如何用linq查询创建Crystal Report的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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