DataSet 不支持 System.Nullable<>c#中的异常 [英] DataSet does not support System.Nullable<> exception in c#

查看:17
本文介绍了DataSet 不支持 System.Nullable<>c#中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public partial class Form2 : Form
{
        public Form2()
        {
            InitializeComponent();
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            RST_DBDataContext db = new RST_DBDataContext();
            var d = (from s in db.TblSpareParts
                                        select new {  s.SPartName, s.SPartCode, s.ModelID, s.SPartLocation,  s.SPartActive, s.SPartSalePrice }).ToArray();
            CrystalReport1 c = new CrystalReport1();
            c.SetDataSource(d);
            crystalReportViewer1.ReportSource = c;

        } 
}

我正在尝试生成水晶报告由于在 c.SetDataSource(d) 处,sql 表中的 SPartSalePrice 可以为空;异常来了请解决

i am trying generate crystal report there in sql table SPartSalePrice is nullable due to that at c.SetDataSource(d); exception come please solve it

推荐答案

使用 匿名投影中的 null 合并条件 运算符映射出null:

Use the null coalescing or conditional operators in your anonymous projection to map out the null:

合并:

var d = (from s in db.TblSpareParts
  select new 
  { 
    s.SPartName,
    ...,
    SPartSalePrice = s.SPartSalePrice ?? 0.0,
    ...
  }).ToArray();

有条件的(对空值不是很有用,但对投影其他值很有用)

Conditional (Not really useful for nulls, but useful for projecting other values)

  SPartSalePrice = s.SPartSalePrice == null ? 0.0 : s.SPartSalePrice,

该字段需要命名(我保留了原来的名称,SPartSalePrice),以及替换的type(0.0) 应该匹配字段的类型.

The field needs to be given a name (I've kept the original one, SPartSalePrice), and the type of substitution (0.0) should match the type of the field.

这篇关于DataSet 不支持 System.Nullable<>c#中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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