Linq 加载相关实体而不使用实体框架核心中的包含
我打算加载相关实体,而不使用C/Entity框架中的include。 在我的示例中,我使用left join来加载所有问题选项,这些选项只有在LINQ查询中的select语句中明确表示时才会显示。我的问题是,如何确保它在不在select INDER LINQ查询中定义相关实体的情况下加载相关实体 数据模型Linq 加载相关实体而不使用实体框架核心中的包含,linq,entity-framework-core,Linq,Entity Framework Core,我打算加载相关实体,而不使用C/Entity框架中的include。 在我的示例中,我使用left join来加载所有问题选项,这些选项只有在LINQ查询中的select语句中明确表示时才会显示。我的问题是,如何确保它在不在select INDER LINQ查询中定义相关实体的情况下加载相关实体 数据模型 数据库会话中较慢的一个元素是将选定数据从数据库管理系统传输到进程。明智的做法是不要向流程发送超出实际计划使用的数据 显然,你有一系列的问题,其中每个问题都有零个或多个问题选项,每个问题选项只属
数据库会话中较慢的一个元素是将选定数据从数据库管理系统传输到进程。明智的做法是不要向流程发送超出实际计划使用的数据 显然,你有一系列的问题,其中每个问题都有零个或多个问题选项,每个问题选项只属于一个问题,即Id等于QuestionOption.QuestionId的问题。一个简单的一对多,在QuestionId中有一个外键 如果您使用10000个QuestionOptions获取Id==4的问题,那么您知道每个QuestionOptions都有一个值为4的QuestionId。您将把值4传输10000次,而您可能甚至不会使用它,因为您已经知道它等于Question.Id 解决方案是:仅在计划更新数据库项时使用Include。 在所有其他情况下,请使用Select。 仅选择实际计划使用的属性 如果愿意,可以使用方法语法或类似的查询语法
var result = context.Questions.Join(context.QuestionOptions, // join Questions and QuestionOptions
question => question.Id, // from every Question take the Id
questionOption => questionOption.QuestionId, // from every Option take the QuestionId
(question, questionOption) => new // when they match make a new object
{ // Select only the properties you plan to use:
Question = new
{
Id = question.Id,
... other question properties
},
Option = new
{
Id = questionOption.Id,
// not needed: questionOption.QuestionId, it equals Question.Id
... other properties you plan to use
}
});
只有当您确实计划使用所有获取的元素时,才需要列出。尽可能长时间地保持您的结果不可更改
如果你想用问题选项来考虑每个问题,请考虑使用
是否要加载包含或不包含的实体?我要加载不包含的实体!如果您使用的是EF Core 2.1,那么可以启用延迟加载,并且在显式调用它时将加载相关实体。非常感谢这篇文章对我的思考和方法有所帮助。。。最后一件事。。。GroupBy如何帮助加快??GroupBy?你是说集体加入?如果一个有1000个选项的问题是普通联接中的查询,那么该问题的属性发送了多少次?如果你用GroupJoin的1000个选项问一个问题,你会问多少次?
var q1 = (
from question in Context.Questions
join options in Context.QuestionOptions on question.Id equals options.QuestionId into qo
where question.ConsultationId == Guid.Parse("10324003-0012-4D99-95D8-7E7189CA3888")
select new
{
question
//,qo // it only loads questionOption if qo is here, I need to do without that, since it is collection property in QuestionDataModel class
}
).ToList();
var result = context.Questions.Join(context.QuestionOptions, // join Questions and QuestionOptions
question => question.Id, // from every Question take the Id
questionOption => questionOption.QuestionId, // from every Option take the QuestionId
(question, questionOption) => new // when they match make a new object
{ // Select only the properties you plan to use:
Question = new
{
Id = question.Id,
... other question properties
},
Option = new
{
Id = questionOption.Id,
// not needed: questionOption.QuestionId, it equals Question.Id
... other properties you plan to use
}
});
var questionsWithTheirOptions = context.Questions
.GroupJoin(context.QuestionOptions, // GroupJoin Questions and QuestionOptions
question => question.Id, // from every Question take the Id
questionOption => questionOption.QuestionId, // from every Option take the QuestionId
(question, optionsOfQuestion) => new // when they match make a new object
{ // desired Question Properties
Id = question.Id,
...
// The options of this question:
Options = optionsOfQuestion.Select(option => new
{
Id = questionOption.Id,
// not needed: questionOption.QuestionId, it equals Question.Id
...
})
.ToList()
});