Arifullah Irfann

Arifullah Irfann

  • NA
  • 410
  • 26.5k

How to Group by using Automapper

Dec 18 2022 12:47 PM

I want to Group by By EmployeeName and DepartmentName 
also in employeeChargesDto By ChargeType Count the ChargeCount and Sum the TotalAmount
I don't know how to do it using automapper
I Have The Following Tables

public partial class Employee
    {
        [Key]
        public int EmployeeDetialID { get; set; }
        public string EmployeeName { get; set; }
        public string DepartmentName { get; set; }           
        public ICollection<FoodCheck> FoodChecks { get; set; }

    }
    
     public class FoodCheck 
    {
        [Key]
        public int FoodCheckID { get; set; }
        public int EmployeeDetialID { get; set; }
        public virtual Employee Employee { get; set; }       
        [Column(TypeName = "date")]
        public DateTime CheckDate { get; set; }
        public ICollection<EmployeeCharge> EmployeeCharges { get; set; }

    }
    
      public class EmployeeCharge
    {
        [Key]
        public int ChargeID { get; set; }
        public int FoodCheckID { get; set; }
        public virtual FoodCheck FoodCheck { get; set; }
        public string ChargeType { get; set; }      
        public int ChargeCount { get; set; }       
        public decimal TotalAmount { get; set; }
       
    }

The Query

var query = _context.FoodChecks
                .OrderByDescending(x => x.FoodCheckID)                
                .ProjectTo<FoodCheckReportDto>(_mapper.ConfigurationProvider)
                .ToListAsync();

Dtos and Mapping

public class FoodCheckReportDto
    {

        public int EmployeeDetialID { get; set; }
        public string EmployeeName { get; set; }
        public string DepartmentName { get; set; }     
        public ICollection<EmplyeeChargeDto> EmployeeCharges { get; set; }

    }

public class EmployeeChargeDto
    {
        public string ChargeType { get; set; }
        public int ChargeCount { get; set; }
        public decimal TotalAmount { get; set; }
    }

CreateMap<FoodCheck, FoodCheckReportDto>()
           .ForMember(d => d.EmployeeName, o => o.MapFrom(s => s.EmployeeName))
           .ForMember(d => d.DepartmentName, o => o.MapFrom(s =>s.DepartmentName))
           .ForMember(d => d.EmployeeCharges, o => o.MapFrom(s => s.EmployeeCharges));

The Output

[
    {
        "employeeDetialID": 7,
        "employeeName": "Ahamd", 
        "departmentName": "Surgery",
        "employeeCharges": [
            {
                "chargeType": "Charged",
                "chargeCount": 1,
                "totalAmount": 220.0000
            }
        ]
    },
    {
       "employeeDetialID": 7,
        "employeeName": "Ahamd",   
        "departmentName": "Surgery",
        "employeeCharges": [
            {
                "chargeType": "Charged",
                "chargeCount": 1,
                "totalAmount": 220.0000
            }
        ]
    },
 {
       "employeeDetialID": 7,
        "employeeName": "Ahamd",   
        "departmentName": "Surgery",
        "employeeCharges": [
            {
                "chargeType": "Paid",
                "chargeCount": 2,
                "totalAmount": 440.0000
            }
        ]
    },
    
]

 


Answers (1)