简体   繁体   English

C# SelectMany 错误“无法隐式转换类型‘System.Collections.Generic.List<char> ' 到 'System.Collections.Generic.List<list> ’”</list></char>

[英]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.ReservationNumberString类型, ReservationNumbersList<string>类型, cIGrouping<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.ReservationNumberString类型,你需要简单地写

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.

相关问题 C#:无法隐式转换类型“System.Collections.Generic.List”<char> &#39; 到 &#39;System.Collections.Generic.List<string> &#39; - C#: Cannot implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<string>' 无法隐式转换类型System.Collections.Generic.List <char> - cannot implicitly convert type System.Collections.Generic.List<char> 无法隐式转换类型&#39;System.Collections.Generic.List错误 - Cannot implicitly convert type 'System.Collections.Generic.List error 无法隐式转换类型&#39;System.Collections.Generic.list <fooClass> &#39;到&#39;System.Collections.Generic.List <T> - Cannot Implicitly convert type 'System.Collections.Generic.list<fooClass>' to 'System.Collections.Generic.List<T> 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List 无法隐式转换类型&#39;System.Collections.Generic.List <MyProduct> &#39;到&#39;System.Collections.Generic.List <T> - Cannot implicitly convert type 'System.Collections.Generic.List<MyProduct>' to 'System.Collections.Generic.List<T> 如何修复“无法将类型System.Collections.Generic.List &lt;&gt;隐式转换为System.Collections.Generic.List &lt;&gt;” - How to fix 'Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>' 无法隐式转换类型'System.Collections.Generic.List<x> ' 到 'System.Collections.Generic.List<y> '</y></x> - Cannot implicitly convert type 'System.Collections.Generic.List<x>' to 'System.Collections.Generic.List<y>' 无法隐式转换类型&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List 无法将类型&#39;System.Collections.Generic.List &lt;&gt;&#39;隐式转换为&#39;&#39; - Cannot implicitly convert type 'System.Collections.Generic.List<>' to ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM