Entity Framework 6 context.Database.SqlQuery

c# entity-framework entity-framework-6 linq sql

Question

Probably a supereasy thing to fix.

I need to make a raw SQL query to the database:

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]")

A DbRawSqlQuery<bool> is returned from this. Trying to convert it to a bool and then using the variable gives me the following error:

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

I don't know what I am doing wrong? I thought you could just cast it to a bool?

Checked the docs here but there's only an example using SqlQuery<string>.

1
1
7/21/2015 10:03:05 AM

Popular Answer

Remove the (bool) as Ehsan told you, then

var res = await blnOrders.SingleAsync();
1
7/21/2015 10:48:50 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow