linq to sql checking for null

前端 未结 4 718
别那么骄傲
别那么骄傲 2020-12-11 17:30

my database field int_ParentId consist of a Null value . how would i check it for a null in this linq query . it is not working

  return _db.Categories.Wher         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 18:27

    This question is actually not easily answered given the lack of context you provide, though usually _db.Categories.Where(m => m.int_ParentId.Equals(null)); does what you want.

    There are a couple of discrepancies between the CTS (.NETs type system) and the SQL type system.

    See SQL-CLR Type Mismatches - MSDN and Null Semantics - MSDN for the full reference.

    Especially null will cause you headaches if you don't take enough care, since it has two completely different meanings in the respective typesystems. NULL in SQL means, "value absent, will match any comparison", whereas null in .NET means "no object, comparing against null will always yield false".

提交回复
热议问题