本文介绍了Linq包括替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像这样从数据库中加载实体

I am a loading a entity from the database like so

    var profileEntity = await Context.Profiles
                                     .Include(x => x.MedicalRecords)
                                     .Include(x => x.DrugHistory)
                                     .Include(x => x.EmploymentStatus)
                                     .SingleOrDefaultAsync(x => x.Id == id);

一切正常,我只是想知道是否有更好的方法来包含其非泛型类型属性,而不是使用 Include方法,因为该特定实体具有很多我需要包含的属性

All is working fine, I was just wondering if there is a better way to include its non generic type properties rather using the Include method because this particular entity has a lot of properties I need to include

推荐答案

不可能自动急切地加载这些属性(用于静态定义导航属性急切加载的机制),但是您可以创建一个为此目的可重用的扩展方法:

It is not possible to automatically eagerly load those properties (Mechanism for statically defining eager loading for navigation properties), but you can create a reusable extension method for this purpose:

public static IQueryable<Profile> IncludeAll(this IQueryable<Profile> query)
{
     return query.Include(x => x.MedicalRecords)
                 .Include(x => x.DrugHistory)
                 .Include(x => x.EmploymentStatus);
}

可以通过以下方式使用:

Which can be used in a following way:

var profileEntity = Context.Profiles.IncludeAll().SingleOrDefault(x => x.Id == id);

这篇关于Linq包括替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 02:11