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:

   var requests = (from job in db.JobRequest
                        join jobCode in db.Job on job.JobCode equals jobCode.PkJobCode
                        join jobParams in db.JobRequestParameter on job.PkJobRequestId equals jobParams.FkJobRequestId into jobRequestParameters
                        from jobParams in jobRequestParameters.DefaultIfEmpty()
                        select new Objects.JobRequest.JobRequest()
                        {
                            JobRequestId = job.PkJobRequestId,
                            JobType = job.FkJobType,
                            JobCode = job.JobCode,
                            Schedule = job.Schedule.AddMilliseconds(-job.Schedule.Millisecond),
                            IsRunning = job.Running,
                            Description = jobCode.BriefDescription,
                            HasParams = ???
                        }).ToList();

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

        SELECT 
      job.pkJobRequestId,
      jobParams.pkJobParameterId,
      (CASE
        WHEN jobParams.pkJobParameterId IS NULL THEN 1 
        ELSE 0
      END) AS HasParams
    FROM
      Job.JobRequest job INNER JOIN
      Reference.Job jobCode ON job.JobCode = jobCode.pkJobCode LEFT OUTER JOIN 
      job.JobRequestParameter jobParams ON job.pkJobRequestId = jobParams.fkJobRequestId 

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

requests = (from job in db.JobRequest
                        join jobCode in db.Job on job.JobCode equals jobCode.PkJobCode
                        join jobParams in db.JobRequestParameter on job.PkJobRequestId equals jobParams.FkJobRequestId into jobRequestParameters
                        from jobParams in jobRequestParameters.DefaultIfEmpty() 
                        select new { pkJobRequestId = job, pkJobParameterId = jobParams == null ? false : true} as blahblah 
                        select new Objects.JobRequest.JobRequest()
                        {
                            JobRequestId = job.PkJobRequestId,
                            JobType = job.FkJobType,
                            JobCode = job.JobCode,
                            Schedule = job.Schedule.AddMilliseconds(-job.Schedule.Millisecond),
                            IsRunning = job.Running,
                            Description = jobCode.BriefDescription,
                            HasParameters = blahblah.PkJobParameterId == null ? true : false
                        }).ToList();

Advertisement

Answer

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

HasParams = ((object)jobParams.FkJobRequestId) == null

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.

var requests = (from job in db.JobRequest
                    join jobCode in db.Job on job.JobCode equals jobCode.PkJobCode
                    select new Objects.JobRequest.JobRequest()
                    {
                        JobRequestId = job.PkJobRequestId,
                        JobType = job.FkJobType,
                        JobCode = job.JobCode,
                        Schedule = job.Schedule.AddMilliseconds(-job.Schedule.Millisecond),
                        IsRunning = job.Running,
                        Description = jobCode.BriefDescription,
                        HasParams = db.JobRequestParameter.Any(jobParams => job.PkJobRequestId == jobParams.FkJobRequestId)
                    }).ToList();

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

var requests = (from job in db.JobRequest
                let jobCode = job.JobCodeEntity // or whatever your nav property is called
                select new Objects.JobRequest.JobRequest()
                    {
                        JobRequestId = job.PkJobRequestId,
                        JobType = job.FkJobType,
                        JobCode = job.JobCode,
                        Schedule = job.Schedule.AddMilliseconds(-job.Schedule.Millisecond),
                        IsRunning = job.Running,
                        Description = jobCode.BriefDescription,
                        HasParams = job.JobParams.Any()
                    }).ToList();
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement