C# 如何在EntityFramework中按相关实体排序

C# 如何在EntityFramework中按相关实体排序,c#,entity-framework,linq,C#,Entity Framework,Linq,我正在开发一个API,它返回有关代表的记录,一切正常,我得到了期望的结果,当我想按相关实体排序时,问题就开始了。 我正在使用实体框架链接这些表 下面是与我的工作相关的DB图的一个片段。 我想在Tier表中按级别订购。 以下是我当前的工作代码: var profiles = _context.Profile .OrderBy(p => p.Person.FirstName) .Include(p => p.Person)

我正在开发一个API,它返回有关代表的记录,一切正常,我得到了期望的结果,当我想按相关实体排序时,问题就开始了。 我正在使用实体框架链接这些表

下面是与我的工作相关的DB图的一个片段。 我想在Tier表中按级别订购。

以下是我当前的工作代码:

 var profiles = _context.Profile
            .OrderBy(p => p.Person.FirstName)
            .Include(p => p.Person)
            .Include(p => p.Person.Address)
            .Include(p => p.Person.Representative.RepresentativeTierHistory)
              .ThenInclude(r => r.Tier)
            .Skip(start)
            .Take(limit);

        var mappedProfiles = _mapper.Map<List<ShortLeaderProfile>>(profiles);
这是我的映射代码:

   public ProfilesProfile()
    {
        MapAddressToLeaderProfile();
    }


    private void MapAddressToLeaderProfile()
    {
        CreateMap<Models.DataModels.Profile, ShortLeaderProfile>()
            .ForMember(lp => lp.Id, opt => opt.MapFrom(p => p.Person.Id))
            .ForMember(lp => lp.FirstName, opt => opt.MapFrom(p => p.Person.FirstName))
            .ForMember(lp => lp.LastName, opt => opt.MapFrom(p => p.Person.LastName))
            .ForMember(lp => lp.PreviousOccupation, opt => opt.MapFrom(p => p.PreviousOccupation))
            .ForMember(lp => lp.Code,
                opt => opt.MapFrom(p =>
                    ActiveTier(p.Person.Representative.RepresentativeTierHistory, DateTime.Now.Date)))
            .ForMember(lp => lp.location, spt => spt.MapFrom(l => l));

        CreateMap<Models.DataModels.Profile, Location>()
            .ForMember(lp => lp.Name, opt => opt.MapFrom(p => p.Person.Address.AddressCityOrTown))
            .ForMember(lp => lp.Latitude, opt => opt.MapFrom(p => p.Latitude))
            .ForMember(lp => lp.Longitude, opt => opt.MapFrom(p => p.Longitude));

    }

    public static string ActiveTier(IEnumerable<RepresentativeTierHistory> representativeTierHistories, DateTime now) =>
        representativeTierHistories?
            .SingleOrDefault(x => x.StartDate <= now && x.EndDate > now)?
            .Tier?
            .Code;
}

最好显示类模型和映射代码。您似乎拥有自己的类型,这在db图中并不明显(甚至缺少
配置文件
)。此外,收藏的单数名称(显然)让人难以理解。您必须解释如何通过多个
Tier
s属性对
Profile
进行排序。Hi@GertArnold,我已经更新了我的问题。谢谢,它是如何工作的,您可以解释它是如何在内部工作的吗?它是这样工作的:对于集合中的每个人,它都会获得最大的层级别,并按此值对人进行排序。关键是,它可以获得最大层级别的单个值,并将其用于排序,而不是Max,也可以是Min。
   public ProfilesProfile()
    {
        MapAddressToLeaderProfile();
    }


    private void MapAddressToLeaderProfile()
    {
        CreateMap<Models.DataModels.Profile, ShortLeaderProfile>()
            .ForMember(lp => lp.Id, opt => opt.MapFrom(p => p.Person.Id))
            .ForMember(lp => lp.FirstName, opt => opt.MapFrom(p => p.Person.FirstName))
            .ForMember(lp => lp.LastName, opt => opt.MapFrom(p => p.Person.LastName))
            .ForMember(lp => lp.PreviousOccupation, opt => opt.MapFrom(p => p.PreviousOccupation))
            .ForMember(lp => lp.Code,
                opt => opt.MapFrom(p =>
                    ActiveTier(p.Person.Representative.RepresentativeTierHistory, DateTime.Now.Date)))
            .ForMember(lp => lp.location, spt => spt.MapFrom(l => l));

        CreateMap<Models.DataModels.Profile, Location>()
            .ForMember(lp => lp.Name, opt => opt.MapFrom(p => p.Person.Address.AddressCityOrTown))
            .ForMember(lp => lp.Latitude, opt => opt.MapFrom(p => p.Latitude))
            .ForMember(lp => lp.Longitude, opt => opt.MapFrom(p => p.Longitude));

    }

    public static string ActiveTier(IEnumerable<RepresentativeTierHistory> representativeTierHistories, DateTime now) =>
        representativeTierHistories?
            .SingleOrDefault(x => x.StartDate <= now && x.EndDate > now)?
            .Tier?
            .Code;
}
[Table(nameof(Profile), Schema = "common")]
[ExcludeFromCodeCoverage]
public class Profile
{
    public int Id { get; set; }
    public string PreviousOccupation { get; set; }
    public string ShortDescription { get; set; }
    public string LongDescription { get; set; }
    [Column(TypeName = "numeric(10, 6)")]
    public decimal? Longitude { get; set; }
    [Column(TypeName = "numeric(10, 6)")]
    public decimal? Latitude { get; set; }
    public int DisplayOrder { get; set; }
    public int PersonId { get; set; }
    [ForeignKey(nameof(PersonId))]
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime RecordStartDateTime { get; set; }
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime RecordEndDatetime { get; set; }
    public List<ProfileSocialMedia> ProfileSocialMedias { get; set; }
    public Person Person { get; set; }
 public class ShortLeaderProfile
{
    public int Id;
    public string FirstName;
    public string LastName;
    public string PreviousOccupation;
    [CanBeNull] public string Code;
    [CanBeNull] public Location location;
}
.OrderBy(p => p.Person.Representative.RepresentativeTierHistory.Max(t => t.Tier.Level))