如何在 dotnet 上进行内部 GroupJoin?

问题描述 投票:0回答:1

我正在将 .NET 6 与实体框架结合使用。这是我的情况: 我有一个 BenefitEntity(具有名称和描述属性以及与 BenefitCategory 的关系),并且这个 BenefitCategory 也具有名称和描述。

在应用程序中,所有名称和描述都已翻译,我们有一个包含所有翻译的 TranslationEntities 表。

这是三个类(我会保持简单,只放置我们需要的属性):

public class BenefitEntity
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public Guid BenefitCategoryId { get; set; }
    public virtual BenefitCategoryEntity CategoryEntity { get; set; }
}

public class BenefitCategoryEntity
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

public class TranslationEntity
{
    public Guid EntityId { get; set; }
    public string Language { get; set; }
    public string Property { get; set; }
    public string Value { get; set; }
}

TranslationEntities 表包含所有实体类型的所有翻译。我想获取所有 BenefitEntity,使用 GroupJoin 作为其名称/描述,并为其 BenefitCategory 名称和描述执行另一个内部联接。我可以做第一个,但做不到第二个。

这就是我所做的:

    public async Task<ICollection<BenefitEntity>> GetAllBenefitEntitiesAsync()
    {
        var benefitsQuery = Entities
            .Include(a => a.CategoryEntity);

        var translationsQuery = DbContext.TranslationsEntities!
            .AsNoTracking()
            .Where(e => e.Language.Equals(LANGUAGE));

        var benefits = await benefitsQuery.ToListAsync();

        var result = benefits
            .GroupJoin(
                translationsQuery,
                benefit => benefit.Id,
                translation => translation.EntityId,
                (benefit, translation) => new { Benefit = benefit, Translation = translation })
            .Select(result => new BenefitEntity
            {
                    // Benefit Name and Description from joined translations
                    Name = result.Translation.FirstOrDefault(e => e.Property.Equals(nameof(BenefitEntity.Name)))?.Value ?? result.Benefit.Name,
                    Description = result.Translation.FirstOrDefault(e => e.Property.Equals(nameof(BenefitEntity.Description)))?.Value ?? result.Benefit.Description,
                    Id = result.Benefit.Id,
                    CategoryEntity = result.Benefit.CategoryEntity,
                    BenefitCategoryId = result.Benefit.BenefitCategoryId,
            })
            .AsQueryable();

        return result.ToList();
}

从这一点开始,我如何使用相同的translationQuery,将categoryEntity.Id与translation.EntityId连接起来,对CategoryEntity进行另一个Join?

c# sql .net entity-framework linq
1个回答
0
投票

在此更新的代码中:

添加了一个新的选择,将翻译具体化到列表中以供多种使用。 新属性 CategoryName 和 CategoryDescription 已添加到 BenefitEntity 以保存 BenefitCategoryEntity 的翻译值。 BenefitCategoryEntity.Name 和 BenefitCategoryEntity.Description 的翻译值是使用相同的翻译逻辑获取的。

 public async Task<ICollection<BenefitEntity>> GetAllBenefitEntitiesAsync() { var benefitsQuery = Entities .Include(a => a.CategoryEntity);

var translationsQuery = DbContext.TranslationsEntities!
    .AsNoTracking()
    .Where(e => e.Language.Equals(LANGUAGE));

var benefits = await benefitsQuery.ToListAsync();

var result = benefits
    .GroupJoin(
        translationsQuery,
        benefit => benefit.Id,
        translation => translation.EntityId,
        (benefit, translation) => new { Benefit = benefit, Translation = translation })
    .Select(result => new
    {
        Benefit = result.Benefit,
        Translation = result.Translation.ToList()
    })
    .Select(result => new BenefitEntity
    {
        // Benefit Name and Description from joined translations
        Name = result.Translation.FirstOrDefault(e => e.Property.Equals(nameof(BenefitEntity.Name)))?.Value ?? result.Benefit.Name,
        Description = result.Translation.FirstOrDefault(e => e.Property.Equals(nameof(BenefitEntity.Description)))?.Value ?? result.Benefit.Description,
        Id = result.Benefit.Id,
        CategoryEntity = result.Benefit.CategoryEntity,
        BenefitCategoryId = result.Benefit.BenefitCategoryId,
        CategoryName = result.Translation.FirstOrDefault(e => e.Property.Equals(nameof(BenefitCategoryEntity.Name)))?.Value ?? result.Benefit.CategoryEntity.Name,
        CategoryDescription = result.Translation.FirstOrDefault(e => e.Property.Equals(nameof(BenefitCategoryEntity.Description)))?.Value ?? result.Benefit.CategoryEntity.Description
    })
    .AsQueryable();`enter code here`

return result.ToList();
}
© www.soinside.com 2019 - 2024. All rights reserved.