I am trying to cast an array of anonymous objects, where each object looks like this:
new {type="internal",title="Linktitle",target="_blank",link="http://www.google.se"}
I have declared a Class "Link", to which the anonymous objects should be casted
class Link{
public string type {get;set;}
public string target {get;set;}
public string title {get;set;}
public string link {get;set;}
}
Now i am trying to cast the objects, like this
List<Link> links = Model.relatedLinks.Select(l => new Link{type=l.type,target=l.target,title=l.title,link=l.link}).ToList();
Then i get the error
Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
I found this page on how to cast anonymous objects, but im doing it the same way. Or have i missed something?
If
relatedLinks
itself is a dynamic value, you've got two problems:Select
andToList
methods.You can work round the first by casting the lambda expression. You can work round the second by calling
Enumerable.Select
directly:Or for the sake of formatting:
If
Model.relatedLinks
is alreadyIEnumerable<dynamic>
(or something similar) then you can callSelect
as an extension method instead - but you still need to have a strongly-typed delegate. So for example, the latter version would become: