how to check null value in linq to sql

c#, linq-to-sql

Solution

You can use `object.Equals`, it will match on `null` values also.

public static IEnumerable<Category> GetChildren(this Table<Category> source, int? parentCategoryId)
{
    var categories = from c in source 
                     where object.Equals(c.ParenCategoryId, parentCategoryId) 
                     select c;
    return categories;
} 

Problem

I am trying to use following linq to sql query to get result. But it doesn't works if parentCategoryId passed as null ``` public static IEnumerable<Category> GetChildren(this Table<Category> source, int? parentCategoryId) { var categories = from c in source where c.ParenCategoryId == parentCategoryId select c; return categories; } ``` but following works if null used directly in place of parentCategoryId ``` public static IEnumerable<Category> GetChildren(this Table<Category> source, int? parentCategoryId) { var categories = from c in source where c.ParenCategoryId == null select c; return categories; } ```

Original source