OR condition is not working in where clause of LINQ query

Hi,

I am using below query to get the employee detail - If there is any fulltime employee exist we need to display that employee if not need to pull default employee.

Hide Copy Code
var employees = new List();

var fulltimeemployees= db.Table().Where(fl => siteId == null || fl.SiteId == siteId).Select(l => l.LanguageId)
employees = db.Table().Where(e => fulltimeemployees.Contains(e.Id) || e.IsDefault).ToList();

return employees ;

But some how even though first condition exist (which means we have full time employee exist) but still it is getting default employee too. and displaying both employee information which is wrong.

I also tried below query but getting run time error when I am excecuting it.

employees = db.Table().Where(e => fulltimeemployees.Where(x=> x.EmployeeId ==e.Id).Any() || e.IsDefault).ToList();

I am new to SQL and LINQ. if I post it in wrong place please let me know. so just want to check what is wrong with above query ? Can anyone help me on this please ?

Thank you in advance.

Run sql profiler and see what query that linq produces and post back here

I don’t have sql profiler . How do I need to tag my post to LINQ . Please help me to fix this issue .

Do you have ssms? And do you have permissions to the sql server

1 Like

Yes I have access to ssms. Thank you so much for helping me on this.

Open your application
Open ssms and point it to the server that your app runs against.
Under Tools Menu you will see profiler choice

Open profiler and follow the prompts to get profiler going

Then run the part of your app that runs the linq you posted

You will see the resulting sql query from linq in sql profiler

Wait a second this looks wrong

var fulltimeemployees= db.Table().Where(fl => siteId == null || fl.SiteId == siteId).Select(l => l.LanguageId)
Above you end by select LanguageId so fulltimeemployees is a list of language Ids.
I dont think that is what you want?

And in the following you go right back to db.Table and query whether the fte list contains e.Id which it always will because fte list comes from db.table from your first query
employees = db.Table().Where(e => fulltimeemployees.Contains(e.Id) || e.IsDefault).ToList()

I think you are better off going to sof?

sorry that is my mistake it is EmployeeId

This is the query

var employees = new List();

var fulltimeemployees= db.Table().Where(fl => siteId == null || fl.SiteId == siteId).Select(e => e. EmployeeId)
employees = db.Table().Where(e => fulltimeemployees.Contains(e.Id) || e.IsDefault).ToList();

return employees ;

Waiting for you to show us what you see in your sql profiler.

Fixed it by adding If else conditions. Thank you so much for your help on this.