代码第一种方法为什么Find()比FirstOrDefault()慢

时间:2014-05-28 06:03:15

标签: asp.net-mvc-4 ef-code-first

我正在使用find()和MyOethDefault()进行MyMethod。两种方法的响应时间不同。查找比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;
        }

使用FirstOrdefault方法

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

请解释一下。感谢

1 个答案:

答案 0 :(得分:0)

在第一种情况下,您正在使用AsEnumerable()加载内存中的所有内容,然后将应用查询。