简体   繁体   中英

Entity Framework Timeout Error

I have this method

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();
         }

This method fails when I call it. A timeout error is returned. However, when I put a breakpoint at the for loop, it passes. Why is this happening?

Thanks,

Sachin

This is beacuse in loop:

foreach(var summaryItinerary in results)

on every element it looks into database. This is enumerable, so access is through each element, and every element iteration checks database. To avoid that do following:

var tmp = results.ToList();
foreach(var summaryItinerary in tmp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM