我使用Dane Morgridge 的存储库代码作为我的存储库类的父级。在父类 EFRepository 中,有一个方法调用 ObjectSet 的 Where 子句并传入 Func 方法。调用此代码并将其分配给我的网格后,该过程需要 4 分钟。但是,如果我对 ObjectSet 的 Where 的调用进行硬编码,则只需三秒钟。任何想法为什么?似乎编译器以某种方式搞砸了。
private void button1_Click(object sender, RoutedEventArgs e)
{
IQueryable<PRODDATA> testP = test.Repository.Find(w => w.PCUST == 49 && w.PDTTK == 20101030);
DateTime firstDate = System.DateTime.Now;
//This is where it takes the most time when passing in the expression above. When the espression is hardcoded (see below) it speeds it up considerably.
radGridView1.ItemsSource = testP;
DateTime secondDate = System.DateTime.Now;
}
public class EFRepository<T> : IRepository<T> where T : PRODDATA
{
public IUnitOfWork UnitOfWork { get; set; }
private IObjectSet<T> _objectset;
private IObjectSet<T> ObjectSet
{
get
{
if (_objectset == null)
{
_objectset = UnitOfWork.Context.CreateObjectSet<T>();
}
return _objectset;
}
}
public virtual IQueryable<T> All()
{
return ObjectSet.AsQueryable();
}
public IQueryable<T> Find(Func<T, bool> expression)
{
//Hardcoding this only takes 2 seconds.
//return ObjectSet.Where(w => w.PCUST == 49 && w.PDTTK == 20101030).AsQueryable();
//passing expression takes 4 minutes.
return ObjectSet.Where(expression).AsQueryable();
}
public void Add(T entity)
{
ObjectSet.AddObject(entity);
}
public void Delete(T entity)
{
ObjectSet.DeleteObject(entity);
}
public void Save()
{
UnitOfWork.Save();
}
}