Linq to sql 重写LinqToSql查询以返回相同的实例,而不使用LoadWith

Linq to sql 重写LinqToSql查询以返回相同的实例,而不使用LoadWith,linq-to-sql,compiled-query,loadoptions,Linq To Sql,Compiled Query,Loadoptions,我正在调查CompiledQuery的性能,但CompiledQuery对LoadWith过敏 using (CustomDataContext myDC = new CustomDataContext()) { DataLoadOptions options = new DataLoadOptions(); options.LoadWith<Customer>(c => c.Orders) myDC.LoadOptions = options; IQuery

我正在调查CompiledQuery的性能,但CompiledQuery对LoadWith过敏

using (CustomDataContext myDC = new CustomDataContext())
{
  DataLoadOptions options = new DataLoadOptions();
  options.LoadWith<Customer>(c => c.Orders)
  myDC.LoadOptions = options;

  IQueryable<Customer> query = myDC.Customers.Where(filterExpr);
  List<Customer> result = query.ToList();
  return result;
}
使用(CustomDataContext myDC=new CustomDataContext())
{
DataLoadOptions=new DataLoadOptions();
options.LoadWith(c=>c.Orders)
myDC.LoadOptions=选项;
IQueryable query=myDC.Customers.Where(filterExpr);
列表结果=query.ToList();
返回结果;
}

此代码通过发出左联接sql查询来填充每个客户实例的Orders属性。如何在没有LoadWith的情况下重写查询以获得相同的结果(客户已填充Orders属性)?

我找到了将DataLoadOptions用于CompiledQuery的方法。

它包括在编译时使DataLoadOptions实例可用,在CompiledQuery实例的生存期内保留该DataLoadOptions实例,并在每次运行CompiledQuery时将该DataLoadOptions替换到位