EF4 添加 is null 子句到 where 子句
EF4 adding is null clause onto where clause
我有以下 linq 查询:
var fileDocuments = (
from doc in fileUploads
from invoice in
(
from inv in _dbContext.SupplierInvoiceHeaders
where inv.InvoiceDocumentId == doc.ID || inv.JobSheetInvoiceId == doc.ID
select inv
).DefaultIfEmpty()
join pos in _dbContext.PurchaseOrders on invoice.PurchaseOrder.PurchaseOrderId equals pos.PurchaseOrderId into poss
from po in poss.DefaultIfEmpty()
join hdf in _dbContext.HelpDeskFaults on po.HelpdeskFaultId equals hdf.ID into hdfpo
from hs in hdfpo.DefaultIfEmpty()
join store1 in _dbContext.Stores on hs.StoreID equals store1.ID into hsf
from hdfStore in hsf.DefaultIfEmpty()
join js in _dbContext.JobSheets on invoice.SupplierInvoiceHeaderId equals js.SupplierInvoiceHeaderID into jss
from jobSheets in jss.DefaultIfEmpty()
join ch in _dbContext.ChildProjects on po.ChildProjectId equals ch.ID into chs
from childProjects in chs.DefaultIfEmpty()
join ph in _dbContext.ProjectHeaders on childProjects.ProjectHeaderID equals ph.ID into phs
from projectHeaders in phs.DefaultIfEmpty()
join ppmsl in _dbContext.PpmScheduleLines on projectHeaders.PPMScheduleRef equals ppmsl.ID into ppsmsls
from ppmScheduleLines in ppsmsls.DefaultIfEmpty()
join ss2 in _dbContext.Stores on ppmScheduleLines.StoreID equals ss2.ID into ssts
from store2 in ssts.DefaultIfEmpty()
select new
{
doc.ID,
JobSheetId = jobSheets.DocumentID,
doc.Name,
doc.DateCreated,
doc.StoreID,
StoreName = doc.Store.Name,
DocumentType = doc.DocumentType.Name,
doc.DocumentTypeID,
HelpDeskFaultStoreName = hs.Store.Name,
DocStoreName = doc.Store.Name,
PPMScheduleLinesStoreName = ppmScheduleLines.Store.Name,
PIR = invoice.PurchaseInvoiceReference
});
fileDocuments = fileDocuments.Where(x => x.PIR == jobSearchParams.PIR);
生成的 where 子句如下所示:
WHERE ([Extent2].[fld_str_PIR] = @p__linq__0) OR (([Extent2].[fld_str_PIR] IS NULL) AND (@p__linq__0 IS NULL))
我不明白为什么要添加 IS NULL 子句。
添加它们是因为您的 jobSearchParams.PIR
可能为空。如果它为空,EF 将假定您想要 return 行,其中 fld_str_PIR
为空。但在 sql 中,您无法使用“=”运算符与 null 进行比较:
WHERE ([Extent2].[fld_str_PIR] = @p__linq__0) -- doesn't work if @p__linq__0 is null
这就是它为您处理这种情况并为两种情况生成正确查询的原因 - 无论 jobSearchParams.PIR
是否为 null。
经过大量搜索,我发现另一个 post 突出显示了 UseDatabaseNullSemantics 设置:
context.Configuration.UseDatabaseNullSemantics = true;
启用此设置后,我的查询将在 900 毫秒内运行而不是超时。
我有以下 linq 查询:
var fileDocuments = (
from doc in fileUploads
from invoice in
(
from inv in _dbContext.SupplierInvoiceHeaders
where inv.InvoiceDocumentId == doc.ID || inv.JobSheetInvoiceId == doc.ID
select inv
).DefaultIfEmpty()
join pos in _dbContext.PurchaseOrders on invoice.PurchaseOrder.PurchaseOrderId equals pos.PurchaseOrderId into poss
from po in poss.DefaultIfEmpty()
join hdf in _dbContext.HelpDeskFaults on po.HelpdeskFaultId equals hdf.ID into hdfpo
from hs in hdfpo.DefaultIfEmpty()
join store1 in _dbContext.Stores on hs.StoreID equals store1.ID into hsf
from hdfStore in hsf.DefaultIfEmpty()
join js in _dbContext.JobSheets on invoice.SupplierInvoiceHeaderId equals js.SupplierInvoiceHeaderID into jss
from jobSheets in jss.DefaultIfEmpty()
join ch in _dbContext.ChildProjects on po.ChildProjectId equals ch.ID into chs
from childProjects in chs.DefaultIfEmpty()
join ph in _dbContext.ProjectHeaders on childProjects.ProjectHeaderID equals ph.ID into phs
from projectHeaders in phs.DefaultIfEmpty()
join ppmsl in _dbContext.PpmScheduleLines on projectHeaders.PPMScheduleRef equals ppmsl.ID into ppsmsls
from ppmScheduleLines in ppsmsls.DefaultIfEmpty()
join ss2 in _dbContext.Stores on ppmScheduleLines.StoreID equals ss2.ID into ssts
from store2 in ssts.DefaultIfEmpty()
select new
{
doc.ID,
JobSheetId = jobSheets.DocumentID,
doc.Name,
doc.DateCreated,
doc.StoreID,
StoreName = doc.Store.Name,
DocumentType = doc.DocumentType.Name,
doc.DocumentTypeID,
HelpDeskFaultStoreName = hs.Store.Name,
DocStoreName = doc.Store.Name,
PPMScheduleLinesStoreName = ppmScheduleLines.Store.Name,
PIR = invoice.PurchaseInvoiceReference
});
fileDocuments = fileDocuments.Where(x => x.PIR == jobSearchParams.PIR);
生成的 where 子句如下所示:
WHERE ([Extent2].[fld_str_PIR] = @p__linq__0) OR (([Extent2].[fld_str_PIR] IS NULL) AND (@p__linq__0 IS NULL))
我不明白为什么要添加 IS NULL 子句。
添加它们是因为您的 jobSearchParams.PIR
可能为空。如果它为空,EF 将假定您想要 return 行,其中 fld_str_PIR
为空。但在 sql 中,您无法使用“=”运算符与 null 进行比较:
WHERE ([Extent2].[fld_str_PIR] = @p__linq__0) -- doesn't work if @p__linq__0 is null
这就是它为您处理这种情况并为两种情况生成正确查询的原因 - 无论 jobSearchParams.PIR
是否为 null。
经过大量搜索,我发现另一个 post 突出显示了 UseDatabaseNullSemantics 设置:
context.Configuration.UseDatabaseNullSemantics = true;
启用此设置后,我的查询将在 900 毫秒内运行而不是超时。