3

我想在一个IEnumerable<dynamic>类型上使用一个 lambda 表达式,但是我在使用新的 lambda 表达式的属性和坐标上得到以下错误:

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.

这是我的代码

public static object returnFullSelectWithCoordinates(IEnumerable<dynamic> q)
        {
            return q.Select(b => new
            {
                route_id = b.b.route_id,
                name = b.b.name,
                description = b.b.description,
                attributes = b.b.route_attributes.Select(c => c.route_attribute_types.attribute_name),
                coordinates = b.b.coordinates.Select(c => new coordinateToSend { sequence = c.sequence, lat = c.position.Latitude, lon = c.position.Longitude })

            });

有什么解决方法可以使我的方法有效吗?

4

1 回答 1

0

由于Select<>是一种扩展方法,因此它不适用于动态类型。

使用 可以得到相同的结果Enumerable.Select<>

试试这个查询:

Enumerable.Select(q,(Func<dynamic,dynamic>)(b => new
        {
            route_id = b.b.route_id,
            name = b.b.name,
            description = b.b.description,
            attributes = b.b.route_attributes.Select(c => c.route_attribute_types.attribute_name),
            coordinates = b.b.coordinates.Select(c => new coordinateToSend { sequence = c.sequence, lat = c.position.Latitude, lon = c.position.Longitude })

        });
于 2013-06-06T09:15:52.600 回答