0

使用 Entity Framework 4.0 生成 SQL 查询的以下两种查询方法中的哪一种明显更快?第一个使用两个“使用”块,而第二个只使用一个。这两种方法都需要两次访问数据库,但第一种方法关闭连接两次,而第二种方法关闭一次。我猜这两种方法的速度大致相同,并且执行速度彼此之间没有显着差异——真的吗?但是,假设这些方法(它们是 Web 服务方法)被非常频繁地访问,大约每秒 2000 次,因此每一点性能都会有所帮助。那么我应该将版本 I 中的代码重写为版本 II 吗?

谢谢你。

 //VERSION I:  TWO USING BLOCKS IN SERIES, THE OUTPUT OF THE FIRST AFFECTING THE SECOND

 List<Categories> CategoryList = new Categories();

 using (EntityFramework1 context = new EntityFramework1())
 {


 try 
 {
 var Cats = from C in context.Categories
      select C);

 CategoryList = Cats.ToList();

 }
 catch (Exception) {}
 }
 }
 } //end of first using block

 using (EntityFramework1 context = new EntityFramework1())
 {

 try 
 {

 Var CustomersFromZipCOde = context.Customers.Where (custo => custo.CustomerID==customerIDString && custo.ZipCode.StartsWith(ZipCodeString) &&      custo.Customer_Categories.Any(categ => CategoryList.Contains(categ.CategoryID)));

 //gives the right output, with CategoryList filled by the earlier query
 }
 catch (Exception) { }
 }
 } //end of second using block


 //VERSION II:  ONE USING BLOCK ONLY--is this significantly faster than Version II?

 List<Categories> CategoryList = new Categories();

 try 
 {

 using (EntityFramework1 context = new EntityFramework1())
 {



 var Cats = from C in context.Categories
      select C);

 CategoryList = Cats.ToList();


 Var CustomersFromZipCOde = context.Customers.Where (custo => custo.CustomerID==customerIDString && custo.ZipCode.StartsWith(ZipCodeString) &&      custo.Customer_Categories.Any(categ => CategoryList.Contains(categ.CategoryID)));

 //gives the right output, with CategoryList filled by the earlier query
 }
 }
 catch (Exception) { }
4

0 回答 0