[英]C# SelectMany error " Cannot Implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<List>' "
Inside of a linq Select
I have a SelectMany
which is throwing an error在 linq
Select
我有一个SelectMany
抛出错误
C# SelectMany error " Cannot Implicitly convert type '
System.Collections.Generic.List<char>
' to 'System.Collections.Generic.List<List>
' "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
automatically converts it to a IEnumerable<char>
, and then ToList
turns that to List<Char>
SelectMany
自动将其转换为IEnumerable<char>
,然后ToList
将其转换为List<Char>
i.ReservationNumber
is of type String
, ReservationNumbers
is of type List<string>
, c
is of type IGrouping<Reservation, int>
i.ReservationNumber
是String
类型, ReservationNumbers
是List<string>
类型, c
是IGrouping<Reservation, int>
类型
What would be a way to force conversion to List<string>
or how to make SelectMany
to return a IEnumerable<string>
强制转换为
List<string>
或如何使SelectMany
返回IEnumerable<string>
的方法是什么
If i.ReservationNumber
is of type String
, you need to write simply如果
i.ReservationNumber
是String
类型,你需要简单地写
ReservationNumbers = c.Select( i => i.ReservationNumber).Distinct().ToList()
SelectMany
tries to iterate through the elements of ReservationNumber
, which, being String
, is an IEnumerable<char>
SelectMany
尝试遍历ReservationNumber
的元素,它是String
,是一个IEnumerable<char>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.