Home > database >  How to access Database to perform Filtering
How to access Database to perform Filtering

Time:12-06

I tried to perform filtering based on Position and Department but I'M getting this error probaply for the way I'm accessing. I tried a different way but the error still the same. I'm access it in wrong way? I Don't know what's the problem

Appreciate your help!

error CS0029: Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.NotFoundObjectResult' to 'dotnet_rpg.Models.ServiceResponse<System.Collections.Generic.List<dotnet_rpg.Dtos.Employee.GetEmployeeDto>>'

Controller

        [HttpGet("search")]

    public async Task<ServiceResponse<List<GetEmployeeDto>>> SearchEmployee(string text) 
        { 

              var response = await _employeeService.searchEmployee(text);
            if (response.Data == null)
            {
                return NotFound(response);
            }
            return Ok(response);
        }

        }
    }

Service Layer


           public async Task<ServiceResponse<List<GetEmployeeDto>>> searchEmployee(string text)
            { 
        

                var response = new ServiceResponse<List<GetEmployeeDto>>();
            var dbEmployee = await _context.Employees
                .Include(a => a.FirstName)
                .Where(p => p.Position.ToLower().Contains(text) ||
                p.Department.ToLower().Contains(text))
                .ToListAsync();
           response.Data = dbEmployee.Select(c => _mapper.Map<GetEmployeeDto>(c)).ToList();
            return response;   

        }

    


DataContext

    public class DataContext : DbContext
    {
        public DataContext(DbContextOptions<DataContext> options) : base(options)
        {
            
        }


   public DbSet<Employee> Employees { get; set; }
        public DbSet<User> Users { get; set; }
     

CodePudding user response:

It looks like the problem is in this line of code:

if (response.Data == null)
{
    return NotFound(response);
}

The NotFound method returns an instance of NotFoundObjectResult, which is a type of ActionResult that indicates that the requested resource was not found.

However, the method signature for SearchEmployee indicates that it should return a ServiceResponse<List<GetEmployeeDto>>, which is a custom type that you have defined in your code. This means that when you try to return the NotFoundObjectResult from the NotFound method, the compiler is unable to convert it to the expected return type and gives you the error message that you see.

One solution to this problem would be to change the return type of the SearchEmployee method to ActionResult<ServiceResponse<List<GetEmployeeDto>>> so that it can return either a ServiceResponse<List<GetEmployeeDto>> or a NotFoundObjectResult. This would allow you to return the result of the NotFound method without any issues.

Here is an example of how your code could be modified to achieve this:

[HttpGet("search")]
public async Task<ActionResult<ServiceResponse<List<GetEmployeeDto>>>> SearchEmployee(string text) 
{ 
    var response = await _employeeService.searchEmployee(text);
    if (response.Data == null)
    {
        return NotFound(response);
    }
    return Ok(response);
}

CodePudding user response:

// replace Task<ServiceResponse<List<GetEmployeeDto>>> to Task<IActionResult>
       [ProducesResponseType(typeof(ServiceResponse<List<GetEmployeeDto>>), StatusCodes.Status200OK)]
        [ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)]
        [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]

 public async Task<IActionResult> SearchEmployee(string text) 
        { 

              var response = await _employeeService.searchEmployee(text);
            if (response.Data == null)
            {
                return NotFound(response);
            }
            return Ok(response);
        }

        }
    }
  • Related