Raso
Raso
CC#
Created by Raso on 5/9/2024 in #help
How do I do an inner GroupJoin on dotnet?
Here is what I did:
C#
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();
}
C#
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();
}
Starting from that point, how can I do another Join for CategoryEntity, using the same translationQuery, joining the categoryEntity.Id with translation.EntityId ?
2 replies