linq - How can I return if an optional navigation property exists from an Entity Framework Query? -


i'm trying return boolean value using following query:

var q = inmate in context.inmates         select new         {             inmate.id,             iscrazy = inmate.certified != null         }; 

iscrazy should true when optional certified navigation property not null. however, iscrazy being returned true, regardless of whether there's link betweeninmate > certified.

using above code , following data:

inmate { 1 } --> { certified } inmate { 2 } --> null inmate { 3 } --> { certified } 

i expecting following results:

1, true 2, false 3, true 

however, results come true. doing wrong?

i tried bring optional navigation property instead, appears inner join , only returns crazy inmates:

inmate { 1 } --> { certified } inmate { 3 } --> { certified } // inmate 2 missing 

edit: forgot mention, using ef 4.0 code first.

edit 2:

this sql output

select  [extent1].[id] [id],  case when (cast(1 bit) <> cast(0 bit))  cast(1 bit) when (1 = 0) cast(0 bit) end [c1] [dbo].[inmate] [extent1] 

looks totally wrong me; there's no mentioned of certified whatsoever.

edit 3:

i tried following code in linqpad (dropping inmate thing, actual code):

from in ingredients join m in meats      on new { i.ingid, i.versionid } equals new { m.ingid, m.versionid } temp t in temp.defaultifempty() select new {     ingid = i.ingid,     ismeat = t.meattypeid == null ? false : true }; 

this return 3000 results correct true/false values. same code in entity framework return results have one-to-one relationship fulfilled.

this sql generated linqpad:

-- region parameters declare @p0 int set @p0 = 0 declare @p1 int set @p1 = 1 -- endregion select [t0].[ingid],      (case          when ([t1].[meattypeid]) null @p0         else @p1      end) [ismeat] [ingredient] [t0] left outer join [meatingredient] [t1] on ([t0].[ingid] = [t1].[ingid])      , ([t0].[versionid] = [t1].[versionid]) 

this sql generated ef:

select  [extent1].[ingid] [ingid],  cast(1 bit) [c1]  [dbo].[ingredient] [extent1] inner join [dbo].[meatingredient] [extent2]      on ([extent1].[versionid] = [extent2].[versionid])          , ([extent1].[ingid] = [extent2].[ingid]) 

well, i've managed working expected thinking backwards. i.e. checking nullable meat has ingredient (or still initial example, check certificate has valid inmate):

var q = in context.ingredients         let m = i.meat // aka certificate         select new          {             ingid = i.ingid,             ismeat = m.ingredient != null          }; 

the sql nasty such simple query. it's 4 times size have been if i'd written stored procedure myself. however, runs in 57ms, it's not worry about.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -