0

I have the following statement:

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

The statement above says that temp is null, but it shouln't be.

T gets as a value a generic List of MyCustomClass like List<MyCustomClass>.

x.bTyp has as a value typeof(IEnumerable<MyCustomClass>)).

The debugger gives me for myCustomList something like this:

bTyp = {System.Collections.Generic.IEnumerable1[...MyCustomClass]}`

How should I rewrite the statement that temp is not null and will return an result ?

RB.
  • 36,301
  • 12
  • 91
  • 131
kkkk00999
  • 179
  • 2
  • 13

1 Answers1

0

Temp is null because there Where() call returns 0 results and FirstOrDefault() is returning the default (which is null).

To debug, place the cursor in the lambda expression and add a breakpoint (default: F9). Run the program.

When the debugger hits the breakpoints, place your boolean expressions ( x.bTyp != null and x.bTyp is IEnumerable) in the watch windows to verify the desired result. Chances are you're missing something here.

Debugger

Robear
  • 986
  • 1
  • 11
  • 15
  • ok, but the name 'x' does not exist in the current context, before, during and after that statement, because its an lambda expression ... – kkkk00999 Apr 07 '16 at 15:28
  • 1
    Are you sure the breakpoint is *inside* of the lambda expression? Are you using Visual Studio? If so, what version?I added an image for clarification. – Robear Apr 07 '16 at 16:09
  • Thank you for the tip with the debugger.When I type `x.bTyp` I get(shortend it) `{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>` hope this helps – kkkk00999 Apr 08 '16 at 06:07