Home > Blockchain >  Data entity filter
Data entity filter

Time:10-25

I have a method where I retrieve data from database and send it . But i need to filter it. I have below code:

using ESM_DASHBOARD.Data;
using ESM_DASHBOARD.Data.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace ESM_DASHBOARD.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class wareh_KPI_INController : ControllerBase
    {
        private readonly EsmDashboardContext _esmDashboardContext;
        public wareh_KPI_INController(EsmDashboardContext esmDashboardContext)
        {
            _esmDashboardContext = esmDashboardContext;
        }
        [HttpGet]
        public IEnumerable<wareh_KPI_IN> Get ()
        {
            return _esmDashboardContext.wareh_KPI_IN;
        }
        [HttpGet("{Week_nb}")]
        public wareh_KPI_IN Get(int Week_nb)
        {

           return _esmDashboardContext.wareh_KPI_IN.Where(s => s.Week_nb == Week_nb);
        }
    }
}

but it diplay error (cannot implicitly convert type system.linq.iorderedenumerable to data.entities.

when i add .FirstOrDefault() i return only 1 value

CodePudding user response:

but it diplay error (cannot implicitly convert type system.linq.iorderedenumerable to data.entities. when i add .FirstOrDefault() i return only 1 value

Because that's exactly what you're telling the code to do. Take a look at the method where you return all of the records:

public IEnumerable<wareh_KPI_IN> Get ()
{
    return _esmDashboardContext.wareh_KPI_IN;
}

What does this method return? An IEnumerable<wareh_KPI_IN>. A collection of records. Now take a look at the method with the error:

public wareh_KPI_IN Get(int Week_nb)
{
    return _esmDashboardContext.wareh_KPI_IN.Where(s => s.Week_nb == Week_nb);
}

What does it return? A wareh_KPI_IN. One record.

If you want to return a collection of records, return a collection of records:

public IEnumerable<wareh_KPI_IN> Get(int Week_nb)
{
    return _esmDashboardContext.wareh_KPI_IN.Where(s => s.Week_nb == Week_nb);
}

Exactly like you already do in your other method.

  • Related