5

次のlinq to sqlクエリを使用して結果を取得しようとしています。ただし、parentCategoryId が 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;
    }   

ただし、parentCategoryId の代わりに null を直接使用すると、次のように動作します

 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;
    }
4

2 に答える 2

8

を使用できます。値にもobject.Equals一致します。null

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;
} 
于 2012-05-05T09:33:35.807 に答える
0

以下を試すことができます

public static IEnumerable<Category> GetChildren(this Table<Category> source, int? parentCategoryId)
{
    var categories = from c in source 
                     where (parentCategoryId != null? c.ParenCategoryId == parentCategoryId : c.ParenCategoryId == null)
                     select c;
    return categories;
}  
于 2012-05-05T09:49:01.723 に答える