Base solution for your next web application
Open Closed

Obtain CreatorUser UserName #2123


User avatar
0
netsolpro created

I am trying to add the Creator UserName to a grid. I can only seem to get the Name. CreatorUserName is the First Name for the APB.User. I am trying to acquire and display the actual UserName for the user that created a record.

Thanks!

public async Task<PagedResultDto<WidgetListDto>> GetWidgets(WidgetFilteredInputDto input)
        {

            var query = _WidgetRepository
                .GetAll()
                .Include(p => p.CreatorUser)
                .AsQueryable();

            var recordCount = await query.CountAsync();

            var records = await query
                .OrderBy(input.Sorting)
                .PageBy(input)
                .ToListAsync();

            var listDto = records.MapTo<List<WidgetListDto>>();

            return new PagedResultDto<WidgetListDto>(
                recordCount,
                listDto);

        }
[AutoMap(typeof(Widget))]
    public class WidgetListDto
    {
        public int Priority { get; set; }

        public string CreatorUserName { get; set; }

        public DateTime CreationTime { get; set; }

        [NotMapped]
        
        [ForeignKey("CreatorUserId")]
        public virtual User User { get; set; }

        public long CreatorUserId { get; set; }

        public string DisplayName

        {
            get

            {

                return User.UserName;
            }
        }
            }

        }
public class Widget : FullAuditedEntity<Guid, User>
 {
        [Required]
        public virtual int Priority { get; set; }
 }

5 Answer(s)
  • User Avatar
    0
    netsolpro created

    I tried something like this. Which populates the query correctly, but then I get mapping errors with my listDto.

    var query = from Widget in _WidgetRepository
    	.GetAll()
    	.Include(p => p.CreatorUser)
    	.AsQueryable()
    	select new
    		{
    		Id = Widget.Id,
    		Priority = Widget.Priority,
    		UserName = Widget.CreatorUser.UserName,
    		CreationTime = Widget.CreationTime
    		};
    
    [AutoMap(typeof(Widget))]
    public class WidgetListDto 
    {
    	public Guid Id { get; set; }
    	public int Priority { get; set; }
    	public DateTime CreationTime { get; set; }
    	
    	[NotMapped]
    	public string UserName { get; set; }
    }
    
  • User Avatar
    0
    exlnt created

    I am very interested in doing the same thing. My application displays create date + create user name and modified date + modified user name on every entity UI page.

    For the index/list view I was able to use the below code, from an ABP example. This joins to the user table and puts the name into my custom "createusername" column in my Dto.

    private IQueryable<CompanyUserAndAllFK> CompanyListingQuery(GetCompanyInput input)
            {
                var query = from cmp in _companyRepository.GetAllCompanies()
                            join listval in _listvaluesRepository.GetAll() on cmp.CurrencyCode equals listval.ListValue
                            join user in _userRepository.GetAll() on cmp.CreatorUserId equals user.Id into userJoin
                            from joinedUser in userJoin.DefaultIfEmpty()
                            select new CompanyUserAndAllFK { Company = cmp, User = joinedUser, ListValues = listval };
    
                query = query
                    .WhereIf(!input.Filter.IsNullOrWhiteSpace(), comp => comp.Company.CompanyName.Contains(input.Filter) || comp.Company.CompanyLegalName.Contains(input.Filter) );
                return query;
            }
    

    I hope this helps?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    @exlnt, thanks for offering a solution. @NetSolPro, your second approach cannot work because you are selection an anonymous object with your linq. Your first approach is correct, but you shouldn't include entities in your Dtos.

    Just define fields like CreatorUserName, CreatorUserSurname, CreatorUserFullName or CreatorUserUserName in your dto and they must be filled with mapping.

  • User Avatar
    0
    netsolpro created

    Thanks @exlnt & @ismcagdas !

    I was able to retrieve the UserName simply by adding CreatorUserUserName to my DTO.

    If this object is mapped(?), where is it inherited from? I am trying to understand how the object CreatorUser is created?

    Thanks!

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Your user class is derived from AbpUser. CreatorUser is here <a class="postlink" href="https://github.com/aspnetboilerplate/module-zero/blob/09cb578f09ee0318b479aa31dd0ceff56a5d218d/src/Abp.Zero/Authorization/Users/AbpUser.cs#L145">https://github.com/aspnetboilerplate/mo ... er.cs#L145</a>.