实体框架6 context.Database.SqlQuery <bool>

时间:2015-07-21 10:03:05

标签: c# sql linq entity-framework entity-framework-6

可能需要修理一个超级好的东西。

我需要对数据库进行原始SQL查询:

var blnOrders = (bool)db.Database.SqlQuery<bool>(@"SELECT 
    CASE WHEN ( EXISTS (SELECT 
        1 AS [C1]
        FROM [dbo].[Orders] AS [Extent1] WITH (TABLOCKX, HOLDLOCK)
        WHERE [Extent1].[OrderNumber] IS NOT NULL
    )) THEN cast(1 as bit) WHEN ( NOT EXISTS (SELECT 
        1 AS [C1]
        FROM [dbo].[Orders] AS [Extent2] WITH (TABLOCKX, HOLDLOCK)
        WHERE [Extent2].[OrderNumber] IS NOT NULL
    )) THEN cast(0 as bit) END AS [C1]
    FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]")

从此返回DbRawSqlQuery<bool>。尝试将其转换为bool,然后使用该变量给出了以下错误:

Cannot implicitly convert type 'System.Data.Entity.Infrastructure.DbRawSqlQuery<bool>' to 'bool'

我不知道自己做错了什么?我以为你可以把它投到bool

检查了文档here,但只有一个使用SqlQuery<string>的示例。

1 个答案:

答案 0 :(得分:1)

按照Ehsan告诉你的那样删除(bool),然后

var res = await blnOrders.SingleAsync();