abp icon indicating copy to clipboard operation
abp copied to clipboard

How to use linq in Repository?

Open wingqing1314 opened this issue 6 years ago • 4 comments

public async Task<List<AdminUserDto> GetAsync(Guid id)
        {
            var data = from a in _adminRep
                       join b in _unitRep
                       on a.UnitId equals b.Id
                       select new AdminUserDto
                       {
                           Id = a.Id,
                           _sex = Sex.female,
                           DepartmentId = a.DepartmentId,
                           UnitId = a.UnitId,
                           UnitName = b.Name
                       };

            return await data.ToListAsync();
        }

image

Any other best-practices for multi-aggregateroot query

wingqing1314 avatar May 19 '20 12:05 wingqing1314

Please read document first. https://docs.abp.io/en/abp/latest/Entity-Framework-Core

maliming avatar May 19 '20 12:05 maliming

Please read document first. https://docs.abp.io/en/abp/latest/Entity-Framework-Core

sorry,I've read it, but I could't find the ritght solution.

so you suggestion is that reference to the Volo.Abp.EntityFrameworkCore package in application?

wingqing1314 avatar May 19 '20 12:05 wingqing1314

https://github.com/abpframework/abp/blob/dev/modules/identity/src/Volo.Abp.Identity.EntityFrameworkCore/Volo/Abp/Identity/EntityFrameworkCore/EfCoreIdentityRoleRepository.cs

wjkhappy14 avatar May 21 '20 09:05 wjkhappy14

Hi @wingqing1314,

When I check your code I see from a in _adminRep, It is wrong usage if the _adminRep is an IRepository.

You should use DbContext in your repository. Do not inject other repositories to another repository, it is not suggested.

Here is an example to you;

var admins = DbContext.Set<Admin>(); // Change the Entity
var units = DbContext.Set<Unit>(); // Change the Entity

var data = from a in admins 
                       join b in units 
                       on a.UnitId equals b.Id
                       select new AdminUserDto
                       {
                           Id = a.Id,
                           _sex = Sex.female,
                           DepartmentId = a.DepartmentId,
                           UnitId = a.UnitId,
                           UnitName = b.Name
                       };

cotur avatar May 21 '20 23:05 cotur