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 to conditional get values on Context ASP.NET Core WebAPI

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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);
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x