Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect syntax generated for select new { bool } #3703

Closed
kenleese opened this issue Nov 10, 2015 · 4 comments
Closed

Incorrect syntax generated for select new { bool } #3703

kenleese opened this issue Nov 10, 2015 · 4 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@kenleese
Copy link

Beta8 / SQL Server 2014
Database model:

public class Blog
{
        public int BlogId { get; set; }
        public string Url { get; set; }
        public bool IsHipster { get; set; } // <=
        public List<Post> Posts { get; set; }
}

Repository model:

public class RepositoryBlog
{
        public int BlogId { get; set; }
        public string Url { get; set; }
        public bool IsHippy { get; set; } // <=
        public List<Post> Posts { get; set; }
}

Query 1:

List<RepositoryBlog> x = (from b in db.Blogs
    select new RepositoryBlog
    {
        BlogId = b.BlogId,
        IsHippy = !b.IsHipster, // Note negation
        Url = b.Url
    }).ToList();

Generated SQL:

SELECT [b].[BlogId], [b].[IsHipster] = 0, [b].[Url]
FROM [Blog] AS [b]

SQL Server error:

Incorrect syntax near '='.

Query 2:

List<RepositoryBlog> x = (from b in db.Blogs
  select new RepositoryBlog
  {
      BlogId = b.BlogId,
      IsHippy = b.IsHipster ? false : true, // Attempted workaround
      Url = b.Url
  }).ToList();

Generated SQL:

SELECT [b].[BlogId], CASE
    WHEN [b].[IsHipster]
    THEN CAST(0 AS BIT) ELSE CAST(1 AS BIT)
END, [b].[Url]
FROM [Blog] AS [b]

SQL Server error:

An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.

Twice the fun!

@rowanmiller
Copy link
Contributor

@maumar we thought this may be something you have already fixed

@kenleese
Copy link
Author

Both issues still occur using rc1-final

@smitpatel
Copy link
Member

1> Comparison in select needs to translated to Case statement, they are not supported directly. #3618 hits the same issue.
2> Boolean is stored as BIT in sql server so when it is used in When part of case it needs to be converted to Boolean or compared to 0.

@smitpatel
Copy link
Member

The workaround was bit shorter

List<RepositoryBlog> x = (from b in db.Blogs
  select new RepositoryBlog
  {
      BlogId = b.BlogId,
      IsHippy = b.IsHipster == true ? false : true, // This works
      Url = b.Url
  }).ToList();

Generated SQL: which is correct

SELECT [b].[BlogId], CASE
    WHEN [b].[IsHipster] = 1
    THEN CAST(0 AS BIT) ELSE CAST(1 AS BIT)
END, [b].[Url]
FROM [Blog] AS [b]

@ajcvickers ajcvickers modified the milestones: 1.0.0-rc2, 1.0.0 Oct 15, 2022
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants