【发布时间】: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