简体   繁体   中英

How to conditional get values on Context ASP.NET Core WebAPI

I am trying to get all the notes created by a specific user. The _context.Notes returns all Notes from the table, but I just need the Notes where postedBy == user_gd where user_gd is the parameter of the method.

How am I able to add an if statement that checks if postedBy == user_gd ? And return all notes of the user when done.

Database tables: postedBy = Gd of user table. Table 1: User table, Table 2: Notes table. 在此处输入图像描述

How can I implement this method so it only returns the notes with postedBy as the parameter

public IEnumerable<Note> GetNotes(Guid user_gd)
{
   return _context.Notes;
}

Help would be appreciated.

Your Note model should contain PostedBy property.

Then you can add using System.Linq; and use:

public IEnumerable<Note> GetNotes(Guid user_gd)
{
   return _context.Notes.Where(x => x.PostedBy == user_gd);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM