尝试投射匿名对象Razor时出错

尝试投射匿名对象Razor时出错

本文介绍了尝试投射匿名对象Razor时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试投射一个匿名对象数组,每个对象看起来像这样:

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();

然后我得到了错误

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?

推荐答案

如果relatedLinks本身是动态值,则有两个问题:

If relatedLinks itself is a dynamic value, you've got two problems:

  • lambda表达部分已经报道
  • 扩展方法不能在动态值上调用( as 扩展方法).这会影响SelectToList方法.
  • The lambda expression part as already reported
  • Extension methods can't be called on dynamic values (as extension methods). This affects both the Select and ToList methods.

您可以通过强制转换lambda表达式来解决第一个问题.您可以通过直接调用Enumerable.Select来解决第二个问题:

You can work round the first by casting the lambda expression. You can work round the second by calling Enumerable.Select directly:

// Note: don't use var here. We need the implicit conversion from
// dynamic
IEnumerable<Link> query = Enumerable.Select(Model.relatedLinks,
                              (Func<dynamic, Link>) (l => new Link {
                                                            type = l.type,
                                                            target = l.target,
                                                            title = l.title,
                                                            link = l.link } );
var links = query.ToList();

或者为了格式化:

Func<dynamic, Link> projection = l => new Link {
                                        type = l.type,
                                        target = l.target,
                                        title = l.title,
                                        link = l.link };
IEnumerable<Link> query = Enumerable.Select(Model.relatedLinks, projection);
var links = query.ToList();

如果Model.relatedLinks已经是IEnumerable<dynamic>(或类似名称),则可以调用Select作为扩展方法-但您仍然需要具有强类型的委托.因此,例如,后一个版本将变为:

If Model.relatedLinks is already IEnumerable<dynamic> (or something similar) then you can call Select as an extension method instead - but you still need to have a strongly-typed delegate. So for example, the latter version would become:

Func<dynamic, Link> projection = l => new Link {
                                        type = l.type,
                                        target = l.target,
                                        title = l.title,
                                        link = l.link };
IEnumerable<Link> query = Model.relatedLinks.Select(projection);
var links = query.ToList();

这篇关于尝试投射匿名对象Razor时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:50