Linq to SQL Strange SQL Translation

I have a simple query that generates some odd SQL translations that explode my code when an object is saturated.

from x in DataContext.MyEntities
select new 
{
    IsTypeCDA = x.EntityType == "CDA" 
}

I expect this request should translate to:

SELECT (CASE WHEN [t0].[EntityType] = @p1 THEN 1 ELSE 0 END) as [IsTypeCDA]
...

Instead, I get the following:

SELECT 
(CASE 
    WHEN @p1 = [t0].[EntityType] THEN 1
    WHEN NOT (@p1 = [t0].[EntityType]) THEN 0
    ELSE NULL
 END) AS [IsTypeCDA]
... 

Since I saturate POCO, where it IsTypeCDAis bool, it explodes, stating that I can not appoint null bool.

Any thoughts?

Edit: fixed property names to make sense ...

+3
source share
3 answers
from x in DataContext.MyEntities
select new
{
  IsTypeCDA = x.EntityType == null 
}

C # interpretation (false) or sql (null) interpretation?

This is done in sql so sql interpretation. That's why the funky translation - the operation returns a nullable bool.

, nullool bool bool.

from x in DataContext.MyEntities
select new
{
  IsTypeCDA = ((bool?)(x.EntityType == "CDA")) ?? false
}
+2

Linq to SQL "" , NULL. , , , . , , NULL EntityType. NULL .

, , , .

-, NULL placeholder.

from x in DataContext.MyEntities
select new 
{
    IsTypeCDA = (x.EntityType ?? "") == "CDA" 
}

String.Equals , NULL s.

from x in DataContext.MyEntities
select new 
{
    IsTypeCDA = string.Equals(x.EntityType, "CDA")
}

SQL, , .

+1

I would have IsTypeCDAget only as the property and select into this class:

public class SomeName
{
    public string EntityType { get; set; }
    public bool IsTypeCDA { get { return EntityType == EntityType.CDA; } }
}

...

from x in DataContext.MyEntities
select new SomeName
{
    EntityType = x.EntityType
}
0
source

Source: https://habr.com/ru/post/1793058/


All Articles