zoukankan      html  css  js  c++  java
  • 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  

     

    这五种方法,都是可以借鉴的。

  • 相关阅读:
    ElEmentUI选择器弹出框定位错乱问题解决(弹出框出现在左上角)
    Element中(Notification)通知组件字体修改(Vue项目中Element的Notification修改字体)
    解决谷歌浏览器设置font-family属性不起作用,(css中设置了font-family:没有用)css字体属性没用
    开发环境Vue访问后端接口教程(前后端分离开发,端口不同下跨域访问)
    nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.报错解决
    ssm项目中中文字符乱码
    idea使用PlantUML画类图教程
    org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 3报错解决
    安装anaconda python时只能安装到默认文件夹&& 安装提示文件夹以存在问题
    生产者消费者代码学习,Producer_Consuner
  • 原文地址:https://www.cnblogs.com/sxw117886/p/5626849.html
Copyright © 2011-2022 走看看