从 mysql 数据库中检索数据并过滤检索到的数据的优先级
Priority in retrieving Data from mysql database and filtering the retrieved data
我只想检索数据库中的一列。以下代码确实有效:
这是我在 mysql-table/model-in-EF6
的客户
public partial class customers
{
public customers()
public int CustomerID { get; set; }
public string FullName { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string Image { get; set; }
}
public List<customers> GetAllCustomers()
{
return myContext.customers.ToList();
}
这是我的问题:
var GetOneColumn = myContext.CustomerRepository.GetAllCustomers().Select(f=>f.FullName);
它是否从数据库中检索客户的所有列,然后 select 仅从检索到的数据中检索一列 (FullName),它是否仅从数据库检索一列 (FullName)?如果它从数据库中检索所有数据,正确的代码是什么 (Linq)?
我怎么能找到那个??
由于您使用的是 .ToList()
EF 将
- 从数据库中检索所有客户
- 将它们映射到客户对象
- 稍后,当您计算
GetOneColumn
时,您会对它们进行投影(遍历已经物化的对象列表)
只检索一列,
- 从存储库中删除
.ToList()
,并且 return IQueryable<Customers>
- 在你的select
var GetOneColumn = myContext.CustomerRepository.GetAllCustomers().Select(f=>f.FullName).ToList();
之后调用.ToList()
因此,您的代码将是
public IQueryable<customers> GetAllCustomers()
{
return myContext.customers;
}
// later in code
var GetOneColumn = myContext.CustomerRepository.GetAllCustomers().Select(f=>f.FullName).ToList();
看看你自己发生了什么!将您的代码分解为多个步骤 debug:
var allCustomers = myContext.CustomerRepository.GetAllCustomers();
var allCustomerNames = allCustomers.Select(f=>f.FullName);
或者,运行 数据库上的分析器,或在 EF
中启用查询日志记录
要查看 EF 生成的所有查询,您可以这样做
using (var context = new BlogContext())
{
context.Database.Log = Console.Write;
// Your code here...
}
在 docs and Log Queries executed by Entity Framework DbContext
中查看更多详细信息
如果您读到这里,那么有必要了解什么实际上会导致 EF 发送查询 - 请参阅 How Queries Work
基本上,只要您开始枚举 IQueryable<T>
的元素(包括 LINQ 方法,如 First()
、Last()
、Single()
、ToList()
, ToArray()
, 等等)
我只想检索数据库中的一列。以下代码确实有效:
这是我在 mysql-table/model-in-EF6
的客户 public partial class customers
{
public customers()
public int CustomerID { get; set; }
public string FullName { get; set; }
public string Mobile { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string Image { get; set; }
}
public List<customers> GetAllCustomers()
{
return myContext.customers.ToList();
}
这是我的问题:
var GetOneColumn = myContext.CustomerRepository.GetAllCustomers().Select(f=>f.FullName);
它是否从数据库中检索客户的所有列,然后 select 仅从检索到的数据中检索一列 (FullName),它是否仅从数据库检索一列 (FullName)?如果它从数据库中检索所有数据,正确的代码是什么 (Linq)?
我怎么能找到那个??
由于您使用的是 .ToList()
EF 将
- 从数据库中检索所有客户
- 将它们映射到客户对象
- 稍后,当您计算
GetOneColumn
时,您会对它们进行投影(遍历已经物化的对象列表)
只检索一列,
- 从存储库中删除
.ToList()
,并且 returnIQueryable<Customers>
- 在你的select
var GetOneColumn = myContext.CustomerRepository.GetAllCustomers().Select(f=>f.FullName).ToList();
之后调用
.ToList()
因此,您的代码将是
public IQueryable<customers> GetAllCustomers()
{
return myContext.customers;
}
// later in code
var GetOneColumn = myContext.CustomerRepository.GetAllCustomers().Select(f=>f.FullName).ToList();
看看你自己发生了什么!将您的代码分解为多个步骤 debug:
var allCustomers = myContext.CustomerRepository.GetAllCustomers();
var allCustomerNames = allCustomers.Select(f=>f.FullName);
或者,运行 数据库上的分析器,或在 EF
中启用查询日志记录要查看 EF 生成的所有查询,您可以这样做
using (var context = new BlogContext())
{
context.Database.Log = Console.Write;
// Your code here...
}
在 docs and Log Queries executed by Entity Framework DbContext
中查看更多详细信息如果您读到这里,那么有必要了解什么实际上会导致 EF 发送查询 - 请参阅 How Queries Work
基本上,只要您开始枚举 IQueryable<T>
的元素(包括 LINQ 方法,如 First()
、Last()
、Single()
、ToList()
, ToArray()
, 等等)