1 year ago

#380003

test-img

Marcelo Antunes Fernandes

ASP.NET CORE labmda sum subquery

I have 3 entities.

VehicleType has a many vehicles and each vehicle has a many rents. Each rent has TotalKm.

I want to know the sum of TotalKm for all rents for each vehicle type.

I try this:

public async Task<IEnumerable<VehicleTypeDto>> GetAllAsync()
    {
        return await _unitOfWork.
          VehicleTypesRepository.
          GetEntityAsNoTracking().
          Include(entity => entity.Vehicles).ThenInclude(entity => entity.Rents).ThenInclude(entity => entity.Damages).
          Select(entity => new VehicleTypeDto
          {
              Id = entity.Id,
              Code = entity.Code,
              Description = entity.Description,
              TotalKm = entity.Vehicles.Sum(v => v.Rents.Sum(r => r.TotalKm )) ?? 0,
          }).
          OrderBy(entity => entity.Description).
          ToListAsync();
    }

But I have this error:

Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Someone can help me pls?

asp.net-core

lambda

subquery

0 Answers

Your Answer

Accepted video resources