tags:

views:

41

answers:

2

I tried to use linq to get storyboard data in code and try following where clause:

Where(
   delegate (abc<VisualStateGroup, VisualState> xyz) 
   { return (xyz.state.Name == "PopupOpened");}
  )

it gave me error:

An anonymous method expression cannot be converted to an expression tree

how to write the right where clause for this case?

+2  A: 

Use a lambda:

Where(xyz => xyz.state.Name == "PopupOpened");
itowlson
+1  A: 

Just use a lambda expression:

.Where( xyz => xyz.state.Name == "PopupOpened" );

If you don't need the operation as an expression tree, you can also write this as an anonymous delegate, but it would be more verbose.

As @itowlson says, if you are using this in a context where an expression tree is expected, you must supply a lamda, as only lambdas can be converted into expression trees - anonymous delegates cannot.

LBushkin
You can't always write it as an anonymous delegate: indeed that appears to be the problem the poster is having. If his source is IQueryable, he *must* use a lambda rather than an anonymous delegate (because lambdas can be compiled as expressions and delegates can't).
itowlson
@itowlson: Indeed, that's something I overlooked when answering the question. I proceeded under the assumption this was a Linq-to-objects question.
LBushkin