EF Core DbSet "Select" Extension Still Shows Other Columns

I am starting to learn ASP and EF Core and am currently working on a web service.

I already have the following:

  • Model menu with three variables / columns
  • DBContext containing DbSet <'Menu'>
  • MenuController which has an HTTPGet method

Inside the HTTPGet method:

    [HttpGet]
    public List<Menu> Get()
    {
       return db.Menus.Select(p => new Menu
        {
            m_menu = p.m_menu
        }).Distinct().ToList();
    }

      

My goal is to select only 1 column instead of 3 columns and distinguish it. So I am using the .Select extension to show only 1 specific column, which is m_menu .

As I call MenuController, HTTPGet still responds or shows all columns, not just 1 column ( m_menu ). The sample JSON result shows other columns as null and only m_menu matters.

I read some related issues and suggested Serialize JSON but still didn't help me with that.

+3


source to share


1 answer


Get the recipient of your method List<Menu>

and Menu

has 3 properties: m_id

, m_menu

and s_menu

. If you only want to return one column, you get a return List<string>

:



[HttpGet]
public List<string> Get()
{
   return db.Menus.Select(p => p.m_menu ).Distinct().ToList();
}

      

+4


source







All Articles