c# - C# SelectMany 错误“无法隐式转换类型'System.Collections.Generic.List' 到 'System.Collections.Generic.List'"
- '"
问题描述
在 linq 内部,Select
我有一个SelectMany
抛出错误
C# SelectMany 错误“无法将类型 '
System.Collections.Generic.List<char>
' 隐式转换为 'System.Collections.Generic.List<List>
'”
List<Hotel> hotelList = reservations
.GroupBy(i => i.hotelID)
.Select(c => new Hotel()
{
HotelID = c.First().HotelID,
HotelName = c.First().HotelName,
HotelAddress1 = c.First().HotelAddress1,
HotelAddress2 = c.First().HotelAddress2,
HotelAddress3 = c.First().HotelAddress3,
HotelCity = c.First().CCCity,
HotelPostalCode = c.First().CCZip,
ReservationNumbers = c.SelectMany( i => i.ReservationNumber).Distinct().ToList()
}).ToList();
SelectMany
自动将其转换为 a IEnumerable<char>
,然后ToList
将其转换为List<Char>
i.ReservationNumber
属于类型String
,ReservationNumbers
属于类型List<string>
,c
属于类型IGrouping<Reservation, int>
什么是强制转换为List<string>
或如何SelectMany
返回的方法IEnumerable<string>
解决方案
如果i.ReservationNumber
是类型String
,你需要简单地写
ReservationNumbers = c.Select( i => i.ReservationNumber).Distinct().ToList()
SelectMany
尝试遍历 的元素ReservationNumber
,其中,being String
,是IEnumerable<char>
推荐阅读
- javascript - 理解 js 数组/对象
- python - 如何通过同一端口python为多个视频创建rtsp?
- google-kubernetes-engine - GKE 大使 http -> https 重定向、健康检查问题
- android - 如何在 Android 中将 Button 放置在 WebView 的顶部
- f# - 为什么在这里泛化具有显式类型注释的值?
- swift - Swift - 更新现有核心数据实体的最佳实践
- angular - 类型对象的角度参数不可分配给参数 Never
- python - 需要一些帮助来创建有效的搜索功能
- python - 如何画出keras CNN架构?
- sql - 使用池连接 SQL 服务器数据库,查询错误,字段使用重音