2

I am doing this MyMethod with find() and same with FirstOrDefault(). The response time of both method differ. Find is slower then FirstOrDefault.

public SelectList MyMethod(Guid UID) {
            List<SelectListItem> selectItems = dbContext.AppMaps.Find(UID).AppMapTerms.AsEnumerable().Select(s => new SelectListItem() {
                Text = s.TermName + " (" + s.StartPeriod.ToShortDateString() + " - " + s.EndPeriod.ToShortDateString() + ")",
                Value = s.UID.ToString(),
                Selected = UID == s.UID ? true : false
            }
          ).ToList();
            selectItems.Insert(0, new SelectListItem() {
                Text = " --Select -- ",
                Value = null,
                Selected = false
            });
            SelectList selectList = new SelectList(selectItems, "Value", "Text");
            return selectList;
        }

With FirstOrdefault method

 public SelectList MyMethod(Guid UID) {
           AppRepository oAppRepository = new AppRepository();
           List<SelectListItem> selectItems = oAppRepository.AllIncluding().FirstOrDefault(e => e.UID == UID).AppTerms.AsEnumerable().Select(s => new SelectListItem() {
               Text = s.TermName + " (" + s.StartPeriod.ToShortDateString() + " - " + s.EndPeriod.ToShortDateString() + ")",
               Value = s.UID.ToString(),
               Selected = UID == s.UID ? true : false
           }
         ).ToList();
           selectItems.Insert(0, new SelectListItem() {
               Text = " --Select -- ",
               Value = null,
               Selected = false
           });
           SelectList selectList = new SelectList(selectItems, "Value", "Text");
           return selectList;
       }

Please Explain. Thanks

sagar43
  • 3,341
  • 3
  • 29
  • 49
Vicxx
  • 444
  • 4
  • 10
  • 2
    Question has been answered [here][1]. [1]: http://stackoverflow.com/questions/14032709/performance-of-find-vs-firstordefault – Kevin Cloet May 28 '14 at 06:12
  • It was not exactly answered as in this case somehow the Find() method seems slower than the FirstOrDefault call which should bethe slower instead. – Márk Gergely Dolinka Jun 06 '14 at 15:57

1 Answers1

0

in the first case you are using AsEnumerable() which loads everything in the memory and after this it will apply the query.

Robert P
  • 137
  • 2
  • 10