gpt4 book ai didi

c# - IEnumreable 动态和 lambda

转载 作者:行者123 更新时间:2023-11-30 18:35:08 25 4
gpt4 key购买 nike

我想在 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 })

});

是否有任何解决方法可以使我的方法起作用?

最佳答案

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 })

});

关于c# - IEnumreable 动态和 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15385260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com