C# SelectMany 错误“无法将类型 'System.Collections.Generic.List<char>' 隐式转换为 'System.Collections.Generic.List<List>'”
C# SelectMany error " Cannot Implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<List>' "
linq 内部 Select
我有一个 SelectMany
抛出错误
C# SelectMany error " Cannot Implicitly convert type
'System.Collections.Generic.List<char>
' to
'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
自动将其转换为 IEnumerable<char>
,然后 ToList
将其转换为 List<Char>
i.ReservationNumber
是String
类型,ReservationNumbers
是List<string>
类型,c
是IGrouping<Reservation, int>
[=33=类型]
强制转换为 List<string>
或如何将 SelectMany
转换为 return 的方法是什么 IEnumerable<string>
如果i.ReservationNumber
的类型是String
,则只需写成
ReservationNumbers = c.Select( i => i.ReservationNumber).Distinct().ToList()
SelectMany
尝试遍历 ReservationNumber
的元素,String
是 IEnumerable<char>
linq 内部 Select
我有一个 SelectMany
抛出错误
C# SelectMany error " Cannot Implicitly convert type '
System.Collections.Generic.List<char>
' to '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
自动将其转换为 IEnumerable<char>
,然后 ToList
将其转换为 List<Char>
i.ReservationNumber
是String
类型,ReservationNumbers
是List<string>
类型,c
是IGrouping<Reservation, int>
[=33=类型]
强制转换为 List<string>
或如何将 SelectMany
转换为 return 的方法是什么 IEnumerable<string>
如果i.ReservationNumber
的类型是String
,则只需写成
ReservationNumbers = c.Select( i => i.ReservationNumber).Distinct().ToList()
SelectMany
尝试遍历 ReservationNumber
的元素,String
是 IEnumerable<char>