第二部分:查询不同是实体
第一种:Linq To Entitiess
static void Main(string[] args)
{
DateTime time1;
DateTime time2;
NorthwindEntities context = new NorthwindEntities();
//先将Customers所有的主键查询出来
List keys = context.Customers.Select(c => c.CustomerID).Distinct().ToList();
//和上面的context以作区别
NorthwindEntities context_ = new NorthwindEntities();
time1 = DateTime.Now;
foreach (var key in keys)
{
var data = context_.Customers.Where(c => c.CustomerID == key).FirstOrDefault();
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2 - time1).ToString());
}
{
DateTime time1;
DateTime time2;
NorthwindEntities context = new NorthwindEntities();
//先将Customers所有的主键查询出来
List keys = context.Customers.Select(c => c.CustomerID).Distinct().ToList();
//和上面的context以作区别
NorthwindEntities context_ = new NorthwindEntities();
time1 = DateTime.Now;
foreach (var key in keys)
{
var data = context_.Customers.Where(c => c.CustomerID == key).FirstOrDefault();
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2 - time1).ToString());
}
查询使用时间0.68秒左右
第二种:使用Entity SQL
static void Main(string[] args)
{
DateTime time1;
DateTime time2;
NorthwindEntities context = new NorthwindEntities();
//先将Customers所有的主键查询出来
List keys = context.Customers.Select(c => c.CustomerID).Distinct().ToList();
//和上面的context以作区别
NorthwindEntities context_ = new NorthwindEntities();
time1 = DateTime.Now;
foreach (var key in keys)
{
var data = context_.Customers.Where("it.CustomerID=@Id", new System.Data.Objects.ObjectParameter("Id", key)).FirstOrDefault();
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2 - time1).ToString());
}
{
DateTime time1;
DateTime time2;
NorthwindEntities context = new NorthwindEntities();
//先将Customers所有的主键查询出来
List keys = context.Customers.Select(c => c.CustomerID).Distinct().ToList();
//和上面的context以作区别
NorthwindEntities context_ = new NorthwindEntities();
time1 = DateTime.Now;
foreach (var key in keys)
{
var data = context_.Customers.Where("it.CustomerID=@Id", new System.Data.Objects.ObjectParameter("Id", key)).FirstOrDefault();
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2 - time1).ToString());
}
查询使用时间0.5秒左右
第三种:使用EntityKey 来查询
static void Main(string[] args)
{
DateTime time1;
DateTime time2;
NorthwindEntities context = new NorthwindEntities();
//先将Customers所有的主键查询出来
List keys = context.Customers.Select(c => c.CustomerID).Distinct().ToList();
//和上面的context以作区别
NorthwindEntities context_ = new NorthwindEntities();
time1 = DateTime.Now;
foreach (var key in keys)
{
var data = context_.GetObjectByKey(new System.Data.EntityKey("NorthwindEntities.Customers", "CustomerID", key)) as Customers;
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2 - time1).ToString());
}
{
DateTime time1;
DateTime time2;
NorthwindEntities context = new NorthwindEntities();
//先将Customers所有的主键查询出来
List keys = context.Customers.Select(c => c.CustomerID).Distinct().ToList();
//和上面的context以作区别
NorthwindEntities context_ = new NorthwindEntities();
time1 = DateTime.Now;
foreach (var key in keys)
{
var data = context_.GetObjectByKey(new System.Data.EntityKey("NorthwindEntities.Customers", "CustomerID", key)) as Customers;
string addr = data.Address;
}
time2 = DateTime.Now;
Console.WriteLine((time2 - time1).ToString());
}
查询时间为0.18秒左右
总结:
通过比较第三种方法仍然比前两种来的快,查询效率更高
使用EntityKey来查询数据比其他两种方法来的更快,而且不是快一点点,而是相差有好几倍,这里的比较并没有使用存储过程来比较查询,但是从这里我们可以看出,在平常的应用中如果知道实体的主键尽量用主键来查询