My database schema is described below:
Form <-> Log
<--->>Seller1
<--->>Seller2
<--->>Seller3
I have a major entity (Form), one to one relationship to another object (Log) And one to many relationship to the childs (Sellers).
I want to pull out all the Forms that one of their Sellers meets certain conditions.
Unfortunately I'm having some problems:
If I run the following routine:
[Test]
public void Can_Get_Forms_Where_CorporationNumber_Is_510778087_Metohd0()
{
var CorporationNumber = "513514950";
var list0 = formRepository
.Where(x => x.Sellers.Any(y => y.CorporationNumber == CorporationNumber))
.Fetch(x=> x.Sellers)
.Fetch(x => x.Log)
.Take(10).ToList();
CollectionAssert.IsNotEmpty(list0);
}
Then I will get sql syntax error
Incorrect syntax near ','
Using NHProf I took the query and found the problem. I reduced my query to Count query to focus on the problem:
select
TOP (
10 /* @p0 */)
cast(
count(
*)
as INT)
as col_0_0_
from BillOfSaleForm form0_
where exists (
select
(
sellers1_.FormID,
sellers1_.tdNum)
from BillOfSaleSeller sellers1_
where form0_.FormID=sellers1_.FormID and
(
(
sellers1_.MisparTagid is null)
and
(
'513514950' /* @p1 */ is null)
or
sellers1_.MisparTagid='513514950' /* @p1 */)
)
We notice that section
select
(
sellers1_.FormID,
sellers1_.tdNum)
Has Extra brackets!.
Of course, if we remove the brackets query executed.