2012-05-05 5 views
5

पर linq में शून्य मान को कैसे जांचें I परिणाम प्राप्त करने के लिए निम्न linq को SQL क्वेरी का उपयोग करने का प्रयास कर रहा हूं। लेकिन यह काम करता है अगर parentCategoryId अशक्तएसक्यूएल

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

के स्थान पर सीधे इस्तेमाल किया है, तो
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; 
    } 
+0

जाँच की कोशिश करता है, तो (ParenCategoryID isEqualTo: शून्य) कर सकते हैं तो आप क्या चाहते हैं। – vishiphone

उत्तर

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; 
} 
+0

ऑब्जेक्ट। एक्सेल का अनुवाद एसक्यूएल में किया जाएगा? यदि हाँ हाँ कैसा दिखता है? – Reniuz

+0

@Reniuz हां यह होगा, यह होगा: 'जहां [टी 0]। [ParenCategoryId] IS NULL' – Magnus

+0

@Reniuz हां है, और यह शून्य से परिवर्तनीय मान के आधार पर लिंक से एसक्यूएल द्वारा अनुकूलित किया गया है: http: //www.brentlamborn .com/post/LINQ-to-SQL-null-check-in-where-Clause.aspx # id_0d234c8d-7ed4-4b1e-97ba-fcc38bb4d277 –

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