我有这个方法
public static List<SummaryItinerary> ReturnBookingsByUserGuid(Guid userGuid)
{
var entities = new gHOPEntities();
var results = from itinerary in entities.Itinerary
where itinerary.UserGuid == userGuid
where itinerary.Booking
select new SummaryItinerary()
{
TourTitle = itinerary.Tours.Title,
TourId = itinerary.Tours.TourId,
TourSEOName =
itinerary.Tours.SEOName,
DepartureDate =
itinerary.DepartureDate,
Passengers = itinerary.Passengers,
Nights = itinerary.Nights,
GrandTotal = itinerary.GrandTotal,
AmountPaid = itinerary.AmountPaid,
CreationDate =
itinerary.CreationDate
};
var summaryItineraryList = new List<SummaryItinerary>();
foreach(var summaryItinerary in results)
{
summaryItineraryList.Add(summaryItinerary);
}
return summaryItineraryList.OrderByDescending(i =>
i.CreationDate).ToList();
}
当我调用它时,此方法失败。返回超时错误。但是,当我在for循环中放置断点时,它会通过。为什么会这样?
谢谢,
萨钦
答案 0 :(得分:0)
这是循环中的问题:
foreach(var summaryItinerary in results)
在它查看数据库的每个元素上。这是可枚举的,因此访问是通过每个元素进行的,每个元素迭代都会检查数据库。为避免这样做:
var tmp = results.ToList();
foreach(var summaryItinerary in tmp)