Asp.net 如何解决';数据集不支持System.Nullable<>';?

Asp.net 如何解决';数据集不支持System.Nullable<>';?,asp.net,asp.net-mvc,entity-framework,Asp.net,Asp.net Mvc,Entity Framework,我的目标是使用crystal report和entity framework将数据导出到pdf文件,但不幸的是,我在尝试运行代码时收到了此错误消息 'System.NotSupportedException:'DataSet不支持System.Nullable' 谁能帮我一下吗 这就是我迄今为止在控制器方面所做的尝试 using System.Data.Entity; using System.IO; using Final_INF271.Reports; using CrystalDecisi

我的目标是使用crystal report和entity framework将数据导出到pdf文件,但不幸的是,我在尝试运行代码时收到了此错误消息

'System.NotSupportedException:'DataSet不支持System.Nullable'

谁能帮我一下吗

这就是我迄今为止在控制器方面所做的尝试

using System.Data.Entity;
using System.IO;
using Final_INF271.Reports;
using CrystalDecisions.CrystalReports.Engine;

public ActionResult Export()
{
    ReportDocument rd = new ReportDocument();
    rd.Load(Path.Combine(Server.MapPath("~/Reports/OutstandingOrders.rpt")));
    rd.SetDataSource(db.ProductOrder.Select(p => new
    {
        p.OrderID,
        p.Date,
        p.SupplierID,
        p.CostPrice,
        p.Quantity
    }).ToList());
    Response.Buffer = false;
    Response.ClearContent();
    Response.ClearHeaders();
    Stream stream = rd.ExportToStream
        (CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
    stream.Seek(0, SeekOrigin.Begin);
    return File(stream, "application/pdf", "OutstandingOrders");
}
包括我的产品订单

namespace Final_INF271.Models
{
    using System;
    using System.Collections.Generic;

    public partial class ProductOrder
    {
        public int OrderID { get; set; }
        public Nullable<System.DateTime> Date { get; set; }
        public Nullable<int> EmployeeID { get; set; }
        public Nullable<int> SupplierID { get; set; }
        public int ProductTypeID { get; set; }
        public Nullable<decimal> CostPrice { get; set; }
        public Nullable<int> Quantity { get; set; }

        public virtual Employee Employee { get; set; }
        public virtual ProductType ProductType { get; set; }
        public virtual Supplier Supplier { get; set; }
    }
}
namespace Final\u INF271.Models
{
使用制度;
使用System.Collections.Generic;
公共部分类ProductOrder
{
公共int-OrderID{get;set;}
公共可空日期{get;set;}
公共可为空的EmployeeID{get;set;}
公共可为空的供应商ID{get;set;}
public int ProductTypeID{get;set;}
公共可为空的成本价{get;set;}
公共可空数量{get;set;}
公共虚拟员工{get;set;}
公共虚拟ProductType ProductType{get;set;}
公共虚拟供应商{get;set;}
}
}
下面是数据集和错误消息的图片


Crystal Reports'
SetDataSource()
方法创建由
ProductOrder
列表提供的
DataColumn
,然后尝试构建类型为nullable的
DataColumn
实例,这是不受支持的

您应该创建一个viewmodel类,该类的属性具有相同的基类型,但不存在可为null的类型,然后将该类作为数据源投影结果:

// Viewmodel
public class ProductOrderVM
{
    public int OrderID { get; set; }
    public DateTime Date { get; set; }
    public int SupplierID { get; set; }
    public decimal CostPrice { get; set; }
    public int Quantity { get; set; }
}

// Controller action
rd.SetDataSource(db.ProductOrder.Select(p => new ProductOrderVM
{
    OrderID = p.OrderID,
    Date = p.Date.GetValueOrDefault(),
    SupplierID = p.SupplierID.GetValueOrDefault(),
    CostPrice = p.CostPrice.GetValueOrDefault(),
    Quantity = p.Quantity.GetValueOrDefault()
}).ToList());
或者,如果可为null的属性具有null值,则使用null合并/三元运算符根据其基类型分配默认值:

rd.SetDataSource(db.ProductOrder.Select(p => new
{
    OrderID = p.OrderID,

    // with ternary operator
    Date = p.Date == null ? DateTime.MinValue : p.Date, // or DateTime.Now as default value

    // with null-coalescing operator
    SupplierID = p.SupplierID ?? 0,
    CostPrice = p.CostPrice ?? 0,
    Quantity = p.Quantity ?? 0
}).ToList());

请包括您的模型类
ProductOrder
,应该有一些可为空的属性导致问题。我已经在where子句中包括了我的模型类ProductOrder如何使用这些运算符?