I am trying to build the Sql Query using by using Sqlkata.I am able to build the correct sql query for one join condition ,but for And Condition i am facing issues
var empDeptQuery = new Query("employee");
empDeptQuery.Select("employee.Name", "dept.Deptname");
empDeptQuery.Join("dept", join => join.On("employee.deptid", "dept.deptid"));
SqlResult empDeptSqlKataQuery = compiler.Compile(empDeptQuery);
Final Query -empDeptSqlKataQuery.Sql is
SELECT [employee].[Name], [dept].[Deptname] FROM [employee]
INNER JOIN [dept] ON ([employee].[deptid] = [dept].[deptid])
i want to add on more condition in join clause like in the following way.
SELECT [employee].[Name], [dept].[Deptname] FROM [employee]
INNER JOIN [dept] ON ([employee].[deptid] = [dept].[deptid] And [employee].[empdeptname]=[dept].[departmentName])
i have tried this but didn't get what i expected
var empDeptQuery = new Query("employee");
empDeptQuery.Select("employee.Name", "dept.Deptname");
empDeptQuery.Join("dept", join => join.On("employee.deptid", "dept.deptid"));
empDeptQuery.Join(empDeptQuery, join => join.On("employee.empdeptname", "dept.departmentName"));
SqlResult empDeptSqlKataQuery = compiler.Compile(empDeptQuery);
Final Sql query with the above approach :
SELECT [employee].[Name], [dept].[Deptname] FROM [employee]
INNER JOIN [dept] ON ([employee].[deptid] = [dept].[deptid])
INNER JOIN (SELECT [employee].[Name], [dept].[Deptname] FROM [employee]
INNER JOIN [dept] ON ([employee].[deptid] = [dept].[deptid])) ON ([employee].[empdeptname] = [dept].[departmentName])
Please let me know how to fix the issue
Use the On
or WhereX
methods as needed, for example
new Query("employee")
.Select("employee.Name", "dept.Deptname")
.Join("dept",
j => j.On("employee.deptid", "dept.deptid")
.On("employee.countryid", "dept.countryid")
)
Note that On
is just an alias for the WhereColumns
method, so all WhereX
methods works in this context also