I have the following linq query (pretty simple):
var jobs = from j in db.jobmsts
join jd in db.jobdtls on j.jobdtl_id equals jd.jobdtl_id
where j.jobmst_type != 1 && j.jobmst_prntid.Equals(folder.FolderID)
orderby j.jobmst_name
select new
{
JobID = j.jobmst_id,
JobName = j.jobmst_name,
Description = j.jobmst_desc,
Source = jd.Source
};
folder.FolderID is a nullable int (int?) that is passed into the method as a parameter. So, when it's null, the query returns no results. But if I execute the same query in TSQL:
select * from jobmst j
join jobdtl jd on j.jobdtl_id = jd.jobdtl_id
where j.jobmst_type != 1 and j.jobmst_prntid is null
I get 6 records. Oddly, if I substitute "folder.FolderID" with "null" (in the linq code), it does return 6 records. So, obviously there's a difference between "null" and an object with a null value. I just don't know what is it and how to fix this.
FYI, I had the code so that the where clause looked like this:
where j.jobmst_type != 1 && j.jobmst_prntid == folder.FolderID
But it didn't produce any results either.
Any help would be appreciated.
As I already mentioned it in a comment: Compare nullable types in Linq to Sql
The answer to that question also applies here: Linq to SQL behaves kind of strangely when querying nullable types. The solution is to check for null
explicitly if the value of the nullable type is null
. If you don't want to write two queries, just write different where clauses (you have to mix in some lambda syntax):
Expression<Func<Job,bool>> predicate;
if(folder.FolderID == null) {
predicate = j=>j.jobmst_prntid == null && j.jobmst_type != 1;
} else {
predicate = j=>j.jobmst_prntid == folder.FolderID && j.jobmst_type != 1;
}
//...
.Where(predicate)
select new
{
// ...
}
I think you have to prepend (int ?)
in the expression to work when you have null
j.jobmst_prntid.Equals ( folder.FolderID )
j.jobmst_prntid.Equals ( (int ? ) folder.FolderID )
For more reference: https://msdn.microsoft.com/en-us/library/bb882535.aspx
or you can try this
where j.jobmst_type != 1 && j.jobmst_prntid.Equals(folder.FolderID== null ? null : folder.FolderID)
I ended up doing this for my situation:
List<Job> folderJobs = new List<Job>();
var tidalJobs = (dynamic) null;
if (folder.FolderID == 0)
{
tidalJobs = from
master in tidal.jobmsts
join
detail in tidal.jobdtls
on
master.jobdtl_id equals detail.jobdtl_id
where
master.jobmst_prntid.HasValue == false && master.jobmst_type != 1
select new
{
JobID = master.jobmst_id,
JobName = master.jobmst_name,
Description = master.jobmst_desc == null ? string.Empty : master.jobmst_desc,
Source = detail.jobdtl_cmd == null ? string.Empty : detail.jobdtl_cmd
};
}
else
{
tidalJobs = from
master in tidal.jobmsts
join
detail in tidal.jobdtls
on
master.jobdtl_id equals detail.jobdtl_id
where
master.jobmst_prntid == folder.FolderID && master.jobmst_type != 1
select new
{
JobID = master.jobmst_id,
JobName = master.jobmst_name,
Description = master.jobmst_desc == null ? string.Empty : master.jobmst_desc,
Source = detail.jobdtl_cmd == null ? string.Empty : detail.jobdtl_cmd
};
}
foreach (var folderJob in tidalJobs)
{
...
}
In short, var tidalJobs = (dynamic) null and .HasValue == false is what I needed to get this working. The only caveat is that setting a var to (dynamic) null disables intellisense b/c everything is evaluated at runtime. So, your typing better be spot on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.