Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Im using EF WEB API in ASP to interact with my SQL Database.

The API works well but id like to change the field my [HttpGet("id")] call queries - so rather returning where id =X , it returns based on another field (eg 'User')

My Model:

namespace CapabilityTrackerAPI.Models
{
public partial class Score
{
public int Id { get; set; } \\this is the primary key
public int User { get; set; }
public int SubmissionPeriod { get; set; }
public int Capability { get; set; }
public byte Score1 { get; set; }
}
}


My Controller Code:
// GET: api/Scores/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Score>> GetScore(int id)
        {
            var score = await _context.Scores.FindAsync(id);

            if (score == null)
            {
                return NotFound();
            }

            return score;
        }



However rather than return the results corresponding to id - id like to retun values that match the 'User'

Any help or guidance would be very much appreciated

thanks

What I have tried:

// GET: api/Scores/5
        [HttpGet("{User}")]
        public async Task<ActionResult<Score>> GetScore(int UserId)
        {
            var score = await _context.Scores.FindAsync(UserId);

            if (score == null)
            {
                return NotFound();
            }

            return score;
        }



But that didn't work - annoyingly

Im not sure if its possible to return based on anything else other than id\Primary Key
Posted
Updated 1-Dec-21 1:06am
v2

If you want to return multiple records, then you need to change the return type of your method.
C#
[HttpGet("id")]
public async Task<ActionResult<IEnumerable<Score>>> GetScore(int id)
{
    var scores = await _context.Scores.Where(x => x.User == id).ToListAsync();
    if (scores.Count == 0) return NotFound();
    return scores;
}
 
Share this answer
 
Ive also tried
FirstOrDefaultAsync
:

// GET: api/Scores/5
        
        [HttpGet("{id}")]
        public async Task<ActionResult<Score>> GetScore(int id)
        {
            //var score = await _context.Scores.FindAsync(id);

            var score = await _context.Scores.FirstOrDefaultAsync(x => x.User == id);
            if (score == null)
            {
                return NotFound();
            }

            return score;
        }



However that only seems to return the first value - if ther are multiple values that match the criteria they are omitted..
 
Share this answer
 
v2
Comments
Richard Deeming 1-Dec-21 7:02am    
If you want to update your question, click the green "Improve question" link and edit your question.

Do not post your update as a "solution" to your question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900