2010-11-16 44 views
0

我發生了一個奇怪的問題。我有一個方法LINQ2SQL基於參數返回項目的問題

public static void ProcessCategories(int? myID) 
{ 
    var tmpList = Adapter.Category.Where(x => x.IdParentCategory == myID).ToList(); 
} 

myID == null(參數),該tmpList不包含任何元素,但如果我鍵入

x.IdParentCategory == null然後返回一些項目。爲什麼?

回答

0

試試這個:

public static void ProcessCategories(int? myID) 
{ 
    var tmpList = Adapter.Category.Where(x => x.IdParentCategory == myID || (myID == null && x.IdParentCategory == null)).ToList(); 
} 
+0

我想這是因爲在SQL中,你必須比較像這樣的空值:其中列爲空。但非空值與= – Thorgeir 2010-11-16 15:59:31

+0

代碼生成一個錯誤'委託'System.Func '不需要1個參數' – Tony 2010-11-16 16:06:57

+0

嗯..我不知道這是爲什麼。但這裏是一個有相同問題的人http://stackoverflow.com/questions/586097/compare-nullable-types-in-linq-to-sql – Thorgeir 2010-11-17 09:03:52