3

使用 F# 查询语法,我试图获取某个字段不为空的所有记录,但我似乎无法找到一种方法。

首先,我尝试了:

query {
    for h in dc.Table do
    where (h.SectorId <> null)
    select h
}

但是出现了一个错误The type 'Nullable<Guid>' does not have 'null' as a proper value. To create a null value for a Nullable type use 'System.Nullable()'.因此,按照建议替换了nullNullable()我使用了:

query {
    for h in dc.Table do
    where (h.SectorId <> Nullable())
    select h
}

当我在 LINQPad 中使用上述查询时,它不会检索任何值,即使我知道它们存在。问题似乎出在创建的 SQL 中:

-- Region Parameters
DECLARE @p0 UniqueIdentifier = null
-- EndRegion
SELECT [t0].[Id], [t0].[Name], [t0].[SectorId], [t0].[Blah], [t0].[Meh], [t0].[DisplayOrder]
FROM [Table] AS [t0]
WHERE [t0].[SectorId] <> @p0

当然这不会起作用,因为NULL <> NULL在 SQL 中总是错误的;应该读的地方WHERE [t0].[SectorId] is not null。如何在 F# 查询中检查 null?

4

1 回答 1

6

尝试

query {
    for h in dc.Table do
    where h.SectorId.HasValue
    select h
}
于 2013-01-29T15:21:19.663 回答