Skip to content
Advertisement

Using LINQ to fill a boolean with true or false based off of condition

I am performing a linq query to grab some data from the DB.

Here is my linq:

The main 2 tables I am concerned with here are JobRequest and JobRequestParameters

JobRequest table has 13 records (but not all jobs have parameters).

JobRequestParameters table only has 4 records. They are joined on by PkJobRequestId. JobRequestParameters has a pkJobParameterId IF and only IF the PkJobRequestID from the JobRequest table has parameters..

How can I modify this linq query to be similar to the SQL I have written below. I would like to be able to fill HasParams with a true if the pkJobParameterId exists, and a 1 if not. Below is my SQL

edit: I have tried this but my syntax is way off.

Advertisement

Answer

It might work, in your original code, to do something like:

But usually when you try to represent LINQ queries as exact translations of their SQL, it ends up overcomplicated. LINQ is a lot more expressive, and can usually represent what you really want better than SQL can. I’d probably do something like this.

If you have navigation properties set up on your object model, you can do even better:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement