2012-11-26 28 views
0

可能重複:
How can i query for null values in entity framework?實體框架選擇其中null是使用 「字段名= NULL」,而不是 「字段名是空」

我使用實體框架爲我的DAL 。 使用SQL事件探查器我已經截獲了數據庫查詢當我運行下面的代碼行

m_Context.DomainEntities.Where(e => e.EntityParentID == i_UnitID); 

i_UnitID等於null,其生成的查詢是:

exec sp_executesql N'SELECT 
[Extent1].[EntityTypeID] AS [EntityTypeID], 
[Extent1].[EntityID] AS [EntityID], 
[Extent1].[EntityName] AS [EntityName], 
[Extent1].[EntityParentID] AS [EntityParentID], 
... 
FROM [dbo].[DomainEntities] AS [Extent1] 
WHERE ([Extent1].[EntityTypeID] IN (CAST(''1'' AS int), CAST(''2'' AS int), CAST(''3'' AS int))) AND ([Extent1].[EntityParentID] = @p__linq__0)',N'@p__linq__0 int',@p__linq__0=NULL 

請注意,這是在where子句中使用[Extent1].[EntityParentID] = @p__linq__0。 那當然dosent工作,我需要它來生成以下

[Extent1].[EntityParentID] is NULL 

是有使用實體框架來實現這一目標的不同方法?

+0

Is is returned the incorrect data? –

+0

是的,有兩個記錄,其中父id等於空,如果我把where子句更改爲'EntityParentID爲null',那麼查詢返回正確的數據。 – Mortalus

+0

重複的鏈接在哪裏? – Mortalus

回答

0

試着寫

m_Context.DomainEntities.Where(e => e.EntityParentID == Convert.ToInt32(i_UnitID)); 

,並檢查生成的SQL查詢。

相關問題