问题描述
我正在尝试使用 Linq to sql 来执行存储过程。直到今天,我一直在使用以下语法,但从今天开始,它开始在整个应用程序中给我一个错误。我收到以下异常。
system.invalidOperationException: 'The cast to value type 'System.Boolean' Failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.'
我使用的代码
return db.Database.sqlQuery<Schviewmodel>("TaskList.TaskListSchedule").ToList();
//View Model
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace TaskList.Models
{
public class Schviewmodel
{
public int TaskListItemID { get; set; }
public Nullable<int> ReportID { get; set; }
public Nullable<int> MemberID { get; set; }
public string ReportName { get; set; }
public string FrequencyType { get; set; }
[DataType(DataType.Date)]
[displayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> DueDate { get; set; }
public string Deadline { get; set; }
public Nullable<int> Duration { get; set; }
public string Day { get; set; }
public string Name { get; set; }
[DataType(DataType.Date)]
[displayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> CompletedDate { get; set; }
public bool Missed { get; set; }
public string OnTime { get; set; }
public string Comments { get; set; }
}
}
解决方法
如果布尔值为空,则需要在存储过程中检查布尔值,则需要在SQL中使用以下条件。
ISNULL(Field,0)
所以它会被转换成布尔类型。