0

このエラー メッセージが表示される理由:

タイプ '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

                    };
4

3 に答える 3

4

LINQステートメントから返されるタイプはIEnumerableであり、リストではありません。

本当にリストとして持っている必要がある場合は、「From a in .... to select new xxx {};)」全体の周りに()を配置し、最初に結果に対してToList()を呼び出します。

それ以外の場合は、「var searchresult = ....」を実行すると、「searchresult」変数でforeachを実行できます。

于 2012-07-27T20:35:18.330 に答える
3

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();
于 2012-07-27T20:35:41.590 に答える
1

リストが必要な場合は、Linqクエリを括弧で囲んで呼び出す必要があります.ToList()

于 2012-07-27T20:36:25.033 に答える