select new
{
countFair = (pstvte.Fair),
});
我已将列Fair的默认值设置为“false”。现在我在新字段中分配这个不可为空的布尔值,它给出了以下错误:
无法将null值赋给System.Boolean类型的成员,该类型是不可为空的值类型
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
答案 0 :(得分:3)
我认为您需要更改countFair
这
bool countFair;
要
bool? countFair;
或
做这样的事情
select new
{
countFair = (pstvte.Fair.HasValue ? pstvte.Fair.Value : false ),
});