C# 使用EF从INCLUDE语句获取特定列
使用INCLUDE语句时,我只想从EF中的查询中获取特定列,而不是返回所有列 此外,如果我还想从我的客户对象返回一个较短的结果集,该怎么办。 有没有办法做到这一点? 我的问题是:C# 使用EF从INCLUDE语句获取特定列,c#,linq,entity-framework,C#,Linq,Entity Framework,使用INCLUDE语句时,我只想从EF中的查询中获取特定列,而不是返回所有列 此外,如果我还想从我的客户对象返回一个较短的结果集,该怎么办。 有没有办法做到这一点? 我的问题是: void Main() { IEnumerable<Customer> customerProjectsList = GetCustomerProjects(); customerProjectsList.Dump(); } public List<Customer> GetC
void Main()
{
IEnumerable<Customer> customerProjectsList = GetCustomerProjects();
customerProjectsList.Dump();
}
public List<Customer> GetCustomerProjects()
{
try
{
using (YeagerTech DbContext = new YeagerTech())
{
var customer = DbContext.Customers.Include("Projects");
return customer;
}
}
catch (Exception ex)
{
throw ex;
}
}
必须使用投影来限制检索的列。在互联网上可以找到许多投影示例一个简短的示例如下:
DateTime twoDaysAgo = DateTime.Now.AddDays(-2);
var groupSummaries = _recipeContext.Groups.OrderBy(g => g.Name)
.Select(g => new GroupSummaryModel{
Id = g.Id,
Name = g.Name,
Description = g.Description,
NumberOfUsers = g.Users.Count(),
NumberOfNewRecipes = g.Recipes.Count(r => r.PostedOn > twoDaysAgo)
});
我从这里取的:
关键是如果需要多次使用新机制。这里它与新的GroupSummaryModel一起使用
编辑
你不一定需要包括;如果您在
客户
和项目
之间具有导航属性,则可以投影到新对象:
var customers = (from c in DbContext.Customers
select new
{
FirstName = c.FirstName,
ProjectName = c.Project.Name
}).ToList().Select(x => new Customer
{
FirstName = x.FirstName,
Project = new Project()
{
Name = x.ProjectName
}
}).ToList();
这将返回一个客户列表
,其中只填充第一个名称,每个客户将包含一个填充名称的项目
属性。这对性能非常好,因为EF发送到数据库的查询很短,并且会快速返回结果集
编辑:
考虑到Projects
是一个ICollection
,我认为最值得维护的事情是创建两个:
您可以使用projectionreplace-ToList()实现这一点;与ToList()一起;Phillip,恕我直言,必须有一个非常简单的解释,为什么我能够以C#语句的形式执行查询,但不能以C#程序的形式执行查询。Philip,我已经使用了ToList()并得到了相同的错误……完全删除ToList(),最后应该只有一个。你有两个。顺便说一下,你现在把你的问题改成了完全不同的问题。你应该从一开始就给出这个细节,或者提出一个新问题。使用ToList的那一刻,它就变成了一个普通的linq查询,不再是IQueryable。因此会出现错误。如果
CustomerDTO
包含列表
hm,则不会编译该列表,但应避免使用toList,否则会将iqueryable与普通linq混淆。您希望查询由服务器而不是客户端处理。循环,如果您在我的第一篇文章中检查我的编辑,您会注意到查询在C#语句中运行良好,但在带有LINQPad的C#程序中运行不好。我所有的模型都设置正确。我将编辑你的帖子并向你展示我的模型。谢谢你的帮助。通告,我想我明白了。。。当我能够发布我的编辑内容时,请查看它,以获得更多关于恢复所有列的帮助。你会在我的编辑中看到我在说什么……循环,我打赌我知道为什么会有额外的专栏出现(即使它们没有人气)。这是因为我的模型中包含该类的所有属性!如果我只在模型中包含我想要的属性,那么只会返回那些属性!非常感谢你的帮助,为我指明了正确的方向。我非常感激。
DateTime twoDaysAgo = DateTime.Now.AddDays(-2);
var groupSummaries = _recipeContext.Groups.OrderBy(g => g.Name)
.Select(g => new GroupSummaryModel{
Id = g.Id,
Name = g.Name,
Description = g.Description,
NumberOfUsers = g.Users.Count(),
NumberOfNewRecipes = g.Recipes.Count(r => r.PostedOn > twoDaysAgo)
});
var qry = (from c in context.Customers
select new CustomerDTO()
{
CustomerId = c.CustomerId,
Projects = (from pr in context.Projects
where c.ProjectId equals pr.Id
select new ProjectDTO
{
Description = pr.Description,
Quote = pr.Quote
}) //--> no tolist here
}).ToList(); //--> only here
var customers = (from c in DbContext.Customers
select new
{
FirstName = c.FirstName,
ProjectName = c.Project.Name
}).ToList().Select(x => new Customer
{
FirstName = x.FirstName,
Project = new Project()
{
Name = x.ProjectName
}
}).ToList();
public CustomerDTO
{
public int CustomerId;
public List<ProjectDTO> projects;
}
public ProjectDTO
{
public string Description;
public string Quote;
}
var qry = (from c in context.Customers
select new CustomerDTO()
{
CustomerId = c.CustomerId,
Projects = (from pr in context.Projects
where c.ProjectId equals pr.Id
select new ProjectDTO
{
Description = pr.Description,
Quote = pr.Quote
}).ToList()
});