【发布时间】:2026-01-25 19:05:02
【问题描述】:
我想在 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 })
});
是否有任何解决方法可以使我的方法有效?
【问题讨论】:
-
A
dynamic作为公共方法的参数看起来不是一个好主意 IMO -
这只是一种测试方法,我想弄清楚如何在匿名类型的 LINQ 查询上为 Select 制作“模板”。
-
错误信息告诉你如何让它工作。 将 lambda 转换为委托或表达式树类型。
标签: c# dynamic ienumerable