[英]Linq to SQL nullable int?
I have the following linq query (pretty simple): 我有以下linq查询(非常简单):
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. folder.FolderID是一个可以为null的int(int?),它作为参数传递给方法。 So, when it's null, the query returns no results. 因此,当它为null时,查询不返回任何结果。 But if I execute the same query in TSQL: 但是如果我在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. 我得到6条记录。 Oddly, if I substitute "folder.FolderID" with "null" (in the linq code), it does return 6 records. 奇怪的是,如果我用“null”(在linq代码中)替换“folder.FolderID”,它确实会返回6条记录。 So, obviously there's a difference between "null" and an object with a null value. 所以,显然“null”和具有null值的对象之间存在差异。 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子句看起来像这样:
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 正如我在评论中已经提到的那样: 将Linq中的可空类型与Sql进行比较
The answer to that question also applies here: Linq to SQL behaves kind of strangely when querying nullable types. 这个问题的答案也适用于此:Linq to SQL在查询可空类型时表现得有些奇怪。 The solution is to check for null
explicitly if the value of the nullable type is null
. 如果可空类型的值为null
则解决方案是显式检查null
。 If you don't want to write two queries, just write different where clauses (you have to mix in some lambda syntax): 如果您不想编写两个查询,只需编写不同的where子句(您必须混合使用一些lambda语法):
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 我认为你必须在表达式中添加(int ?)
才能使用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 有关更多参考: 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. 简而言之,var tidalJobs =(dynamic)null和.HasValue == false是我需要的工作。 The only caveat is that setting a var to (dynamic) null disables intellisense b/c everything is evaluated at runtime. 唯一需要注意的是,将var设置为(dynamic)null会禁用intellisense b / c,所有内容都会在运行时进行评估。 So, your typing better be spot on. 所以,你的打字更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.