2016-04-07 26 views
0

我有以下聲明:Lambda表達式用的valueOf一個通用的IEnumerable

AnotherClass temp = myCustomList 
    .Where(x => x.bTyp != null && x.bTyp is IEnumerable<T>) 
    .FirstOrDefault();` 

上述聲明說,tempnull,但它shouln't是。

T得到一個通用值ListMyCustomClass就像List<MyCustomClass>。作爲值typeof(IEnumerable<MyCustomClass>))

調試器讓我對myCustomList是這樣的:

bTyp = {System.Collections.Generic.IEnumerable 1 ... MyCustomClass]}`

我應該如何改寫聲明tempnull,並會返回一個結果?

+0

你可以檢查對象本身是否爲空而不是類型? – Jacobr365

+0

是的,但它有8個元素('List '),所以它不是'null'。 – kkkk00999

+0

http://stackoverflow.com/questions/2404993/is-it-possible-to-define-a-generic -lambda – willaien

回答

0

Temp爲空,因爲Where()調用返回0結果,而FirstOrDefault()返回默認值(它爲null)。

要進行調試,請將光標置於lambda表達式中並添加一個斷點(默認值:F9)。運行該程序。

當調試器擊中斷點時,請在監視窗口中放置布爾表達式(x.bTyp!= null和x.bTyp爲IEnumerable)以驗證所需的結果。有機會,你在這裏錯過了一些東西。

Debugger

+0

確定,但名稱'x'在當前上下文中不存在,在該語句之前,期間和之後,因爲它的lambda表達式... – kkkk00999

+1

您確定斷點是* lambda表達式?你在使用Visual Studio嗎?如果是這樣,什麼版本?我添加了一個圖像澄清。 – Robear

+0

感謝您使用調試器的提示。當我輸入'x.bTyp'時,我得到(縮短了它)'{System.Collections.Generic.IEnumerable'1 [... MyCustomClass} .... IsGenericType:true .. .UnderlyingSystemType:{Name =「IEnumerable'1」FullName =「System.Collections.Generic.IEnumerable'1 [[... MyCustomClass}'and'T' is'System.Collections.Generic.List <... MyCustomClass> '希望這有助於 – kkkk00999