C# 从mysql数据库中检索数据并过滤检索到的数据的优先级
我只想检索数据库中的一列。以下代码确实有效: 这是我在mysql表/model-in-EF6中的客户C# 从mysql数据库中检索数据并过滤检索到的数据的优先级,c#,entity-framework-6,C#,Entity Framework 6,我只想检索数据库中的一列。以下代码确实有效: 这是我在mysql表/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 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; }
}
它是否从数据库中的客户检索所有列,然后从检索到的数据中仅选择一列(全名),或者是否仅从数据库中检索一列(全名)?如果它从数据库中检索所有数据,那么正确的代码(Linq)是什么
我怎么能找到那个 因为您使用的是
.ToList()
EF
GetOneColumn
时,您会对它们进行投影(遍历已经物化的对象列表).ToList()
,并返回一个IQueryable
var GetOneColumn=myContext.CustomerRepository.GetAllCustomers()之后调用.ToList()
代码>
public IQueryable<customers> GetAllCustomers()
{
return myContext.customers;
}
// later in code
var GetOneColumn = myContext.CustomerRepository.GetAllCustomers().Select(f=>f.FullName).ToList();
或者,在数据库上运行探查器,或者在EF中启用查询日志记录
要查看EF生成的所有查询,可以执行以下操作
using (var context = new BlogContext())
{
context.Database.Log = Console.Write;
// Your code here...
}
请参阅和中的更多详细信息
如果您读到这里,那么就有必要了解到底是什么导致EF发送查询-请参阅
基本上,每当您开始枚举
IQueryable
的元素时(包括像First()
,Last()
,Single()
,ToList()
,ToArray()
等LINQ方法)简单而完整的答案。非常感谢。不客气@ehsan_kabiri_33!很高兴我能帮忙!
public IQueryable<customers> GetAllCustomers()
{
return myContext.customers;
}
// later in code
var GetOneColumn = myContext.CustomerRepository.GetAllCustomers().Select(f=>f.FullName).ToList();
var allCustomers = myContext.CustomerRepository.GetAllCustomers();
var allCustomerNames = allCustomers.Select(f=>f.FullName);
using (var context = new BlogContext())
{
context.Database.Log = Console.Write;
// Your code here...
}