【问题标题】:How do I fix an error message of "Cannot implicitly convert type Generic.IEnumerable<type>' to 'Generic.List<type>"如何修复“无法将类型 Generic.IEnumerable<type>' 隐式转换为 'Generic.List<type>”的错误消息
【发布时间】:2012-07-27 20:32:45
【问题描述】:

为什么我会收到此错误消息:

无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.List”

当我构建这段代码时:

 List<BALHotelList> searchresult=from a in bh
                join b in hr on a.HotelCode equals b.hotelCode
                orderby a.HotelName
                select new BALHotelList
                    {
                       HotelCode= a.HotelCode,
                       ImageURL_Text = a.ImageURL_Text,
                       HotelName = a.HotelName,
                       StarRating = a.StarRating,
                       HotelAddress = a.HotelAddress,
                       Destination = a.Destination,
                       Country = a.Country,
                       HotelInfo = a.HotelInfo,
                       Latitude = a.Latitude,
                       Longitude = a.Longitude,
                       totalPrice = b.totalPrice,
                       totalPriceSpecified = b.totalPriceSpecified,
                       totalSalePrice = b.totalSalePrice,
                       totalSalePriceSpecified = b.totalSalePriceSpecified,
                       rooms = b.rooms

                    };

【问题讨论】:

  • 为什么不直接使用var searchresult =
  • 因为 var 范围仅限于该方法。
  • 重要吗?然后在查询结束时抛出一个.ToList()
  • @rahularyansharma 当var 用于匿名类型时,匿名类型不能离开方法。对于“真实”类型,var 只是用于输入完整类型名称的语法糖。

标签: c# asp.net linq list generics


【解决方案1】:

从 LINQ 语句返回的类型是 IEnumerable,而不是 List。

如果你真的必须将它作为一个列表,那么将 () 放在整个 "From a in.... to select new xxx {};) 周围,然后首先在结果上调用 ToList()。

否则,请执行“var searchresult = ....”,您仍然可以在“searchresult”变量上进行 foreach。

【讨论】:

  • var 仅限于范围,我想在全局范围内使用它
  • 对,照我说的做,在结果 IEnumerable 上显式调用 ToList()
【解决方案2】:

您可能只需要用一些括号包裹您的 linq 并在结果上调用 .ToList:

List<BALHotelList> searchresult= (from a in bh
            join b in hr on a.HotelCode equals b.hotelCode
            orderby a.HotelName
            select new BALHotelList
                {
                   HotelCode= a.HotelCode,
                   ImageURL_Text = a.ImageURL_Text,
                   HotelName = a.HotelName,
                   StarRating = a.StarRating,
                   HotelAddress = a.HotelAddress,
                   Destination = a.Destination,
                   Country = a.Country,
                   HotelInfo = a.HotelInfo,
                   Latitude = a.Latitude,
                   Longitude = a.Longitude,
                   totalPrice = b.totalPrice,
                   totalPriceSpecified = b.totalPriceSpecified,
                   totalSalePrice = b.totalSalePrice,
                   totalSalePriceSpecified = b.totalSalePriceSpecified,
                   rooms = b.rooms

                }).Tolist();

【讨论】:

    【解决方案3】:

    如果您想要一个列表,您需要将 Linq 查询包装在括号中并调用 .ToList()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多