Linq里where出現null的問題


今天遇到一個問題,怎么在where里判斷一個字段是否為null,並且這個字段不是字符串string類型,而是int和GUID類型,折騰了半天終於搞明白了。(由於項目是我半路接手的,問題是前期的同事給我挖了一個坑,我今天就是填坑的)

1.在說這個問題之前,我先說一下數據庫的表與C#之間model的對應:

一般數據庫中的表我們都可以使用代碼生成器(東軟代碼生成器)生成,其中字段的可空不可空也會給我們生成,如:表

用代碼生成器生成的model是:

/// <summary>
    /// SettingConfiguration:實體類(屬性說明自動提取數據庫字段的描述信息)
    /// </summary>
    [Serializable]
    public partial class SettingConfiguration
    {
        public SettingConfiguration()
        {}
        #region Model
        private int _id;
        private string _name;
        private string _value;
        private int? _parentid;
        /// <summary>
        /// 
        /// </summary>
        public int Id
        {
            set{ _id=value;}
            get{return _id;}
        }
        /// <summary>
        /// 
        /// </summary>
        public string Name
        {
            set{ _name=value;}
            get{return _name;}
        }
        /// <summary>
        /// 
        /// </summary>
        public string Value
        {
            set{ _value=value;}
            get{return _value;}
        }
        /// <summary>
        /// 
        /// </summary>
        public int? ParentId
        {
            set{ _parentid=value;}
            get{return _parentid;}
        }
        #endregion Model

    }
View Code

數據庫中我們可以看到ParentId這個字段是可以為空的,其他字段不能為空;

對應到代碼生成器生成的代碼Model就可以看到代碼:

public int? ParentId

{
set{ _parentid=value;}
get{return _parentid;}
}

主要是int?

有些能不知道這個int?是什么意思?和int有什么區別?這里簡單說一下兩者之間的區別:int?是可以為null的,而int是不能賦值null的:如下面兩個代碼:

int? a = null; //編譯成功
int a = null; //編譯錯誤。
int?允許把null賦值給數值型,這個是為了兼容SQL或者其它數據庫中Null這個空值所設定的。

所以:在把數據庫的表轉化成model時,一定要把可為空的字段轉化成對應的可為空的C#字段:

主要有

int——>int?

Guid——>Guid?

DateTime——>DateTime?

short——>short?

decimal——>decimal?

注意:有人說string為什么不轉化成string?  額額額額~~~~~你傻呀,string類型的字段本來就可以為null,何必要寫出string?,多此一舉。

通過上面的介紹,大家一定對數據庫表轉化成model有了一定的了解。那么我們就言歸正傳,說問題:

2.問題的根源

問題的根源就是同是在建model的時候,該轉化的沒轉化,導致我查詢一而再、再而三的失敗。

對於可以為null的字段,如果要判斷這個字段是不是null,比如我們在查詢表SettingConfiguration中ParentId是null的數據,我們就可以這樣寫:

var dtvar = (from des in db.SettingConfiguration
                 where(des.ParentId==null)
      select des);

這樣查詢肯定沒問題的,可以~~~~嘿嘿嘿嘿嘿嘿~~~

如果你在建model的時候ParentId的字段是 

public int ParentId

{
set{ _parentid=value;}
get{return _parentid;}
}

 這樣寫的,那你怎也查不到數據,並且還不會報錯,讓你郁悶終生(我就是這么郁悶的),郁悶郁悶,在網上找了很多方法,都不能查詢出數據,

后來改了一下model之后,什么問題都解決了,這就是一個大坑,讓我填平了,同時在提醒大家,建model一定要和數據庫對應,要不然以后坑的都是自己。

 

下面提供一些很有用的查詢null的方法:

 

LINQ TO SQL   Null 查詢

 

  SELECT *   FROM [Orders] AS [t0]   WHERE ([t0].[ShippedDate]) IS NULL  

 方法一:

from o in Orders  where o.ShippedDate==null  select o  

對應的Lamda表達式為:

Orders .Where (o => (o.ShippedDate == (DateTime?)null))  

對應的SQL語句為:

  SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]   FROM [Orders] AS [t0]   WHERE [t0].[ShippedDate] IS NULL  

方法二:

from o in Orders  where Nullable<DateTime>.Equals(o.ShippedDate,null)  select o  

對應的Lamda表達式為:

Orders .Where (o => Object.Equals (o.ShippedDate, null))  

對應的SQL語句為:

  SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]   FROM [Orders] AS [t0]   WHERE [t0].[ShippedDate] IS NULL  

  方法三: 

from o in Orders  where !o.ShippedDate.HasValue  select o  

對應的Lamda表達式為:

Orders .Where (o => !(o.ShippedDate.HasValue))  

對應的SQL語句為:

  SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]   FROM [Orders] AS [t0]   WHERE NOT ([t0].[ShippedDate] IS NOT NULL)  

 方法四:

from o in Orders  where o.ShippedDate.Value==(DateTime?)null  select o  

對應的Lamda表達式為: 

Orders.Where (o => ((DateTime?)(o.ShippedDate.Value) == (DateTime?)null))  

 對應的SQL語句為:

SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]  FROM [Orders] AS [t0]  WHERE ([t0].[ShippedDate]) IS NULL   

 方法五:

from o in Orders  where  System.Data.Linq.SqlClient.SqlMethods.Equals(o.ShippedDate.Value,null)  select o  

對應的Lamda表達式為:

Orders .Where (o => Object.Equals (o.ShippedDate.Value, null))  

對應的SQL語句為:

  SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]   FROM [Orders] AS [t0]   WHERE ([t0].[ShippedDate]) IS NULL  

 

這五種方法,都是可以借鑒的。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM