无法将null值分配给类型为System.Boolean的成员,该类型是不可为空的值类型

时间:2014-01-22 10:11:17

标签: c# linq nullable

select new
{
    countFair = (pstvte.Fair),
});

我已将列Fair的默认值设置为“false”。现在我在新字段中分配这个不可为空的布尔值,它给出了以下错误:

  

无法将null值赋给System.Boolean类型的成员,该类型是不可为空的值类型

enter image description here

CREATE TABLE [dbo].[PostVote](
    [PostVoteId] [bigint] IDENTITY(1,1) NOT NULL,
    [PostId] [bigint] NOT NULL,
    [UserId] [bigint] NOT NULL,
    [Fair] [bit] NOT NULL,
    [NotFair] [bit] NOT NULL,
 CONSTRAINT [PK_PostVote] PRIMARY KEY CLUSTERED 
(
    [PostVoteId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[PostVote] ADD  DEFAULT ('False') FOR [Fair]
GO

ALTER TABLE [dbo].[PostVote] ADD  DEFAULT ('False') FOR [NotFair]
GO

1 个答案:

答案 0 :(得分:3)

我认为您需要更改countFair

的声明

bool countFair;

bool? countFair;

做这样的事情

select new
{
    countFair = (pstvte.Fair.HasValue ? pstvte.Fair.Value : false ),
});