DLinq not able to generate correct queries

Hi,

I am using DLinq to get all the product categories based on the parent category id, but the framework is giving SqlException "Incorrect syntax near the keyword 'OR'". After logging the output of DataContext to Console, things become more clear. The details are as below:

Here is the c# code which I wrote to get product categories:

Nullable<int> parentCategoryId = 2;
Table<Category> cat = db.GetTable<Category>();
Query<Category> categories;
categories = from c
in cat
where parentCategoryId ==
null || c.ParentCategoryId == parentCategoryId
select c;

Following is the SQL query generated behind the scenes by Query engine and is totally wrong. I think that the Query engine is not able to work with the parameters in complex cases. I am also interested to know how this 'Predicate' words comes into the query and also the logic behind using convert

SELECT [t0].[CategoryId], [t0].[CategoryName], [t0].[ParentCategoryId]
FROM [Category] AS [t0]
WHERE (CONVERT(Predicate,CONVERT(Bit,0))) OR ([t0].[ParentCategoryId] = @p0)

Ideally, it should generate the query like one below:

SELECT [t0].[CategoryId], [t0].[CategoryName], [t0].[ParentCategoryId]
FROM [Category] AS [t0]
WHERE @p0 is Null OR ([t0].[ParentCategoryId] = @p0)

Thanks and Regards,

Navneet Gupta

 




Answer this question

DLinq not able to generate correct queries

  • lockdon9w

    You've found a bug in boolean constant translation. Thanks for pointing it out.

  • DLinq not able to generate correct queries