DataSet不支持System.Nullable<>例外在c# [英] DataSet does not support System.Nullable<> exception in c#

查看:588
本文介绍了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;

        } 
}

我正在尝试生成水晶报告b $ b在sql表中SPartSalePrice是可空的,因为在c.SetDataSource(d);异常来解决它

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 coalescing 条件运算符在您的匿名投影中映射出 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();

有条件的(对于null而言非常有用,但对投影其他值很有用)

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

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

该字段需要给出一个名称(我保留原来的一个, SPartSalePrice )和类型替换( 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天全站免登陆