Search code examples
c#linqasp.net-coreasp.net-core-webapief-core-3.1

Returning a single property of a Model in HttpGet Method in ASP.NET Core Web API


I have to return a single property of a model inside GetAll() method in ASP.NET Core API. Here is my Model:

using System;
using System.Collections.Generic;

namespace ProjectWayneAPI.Models
{
public partial class Consignment
{

    public Guid Id { get; set; }
    public DateTime? DateCreated { get; set; }
    public bool? Deleted { get; set; }
    public string CustomerReference { get; set; }
    public string ConsignmentNote { get; set; }
    public Guid AccountId { get; set; }
    public Guid BookedByUser { get; set; }
    public string DescriptionToLeave { get; set; }

    public virtual Account Account { get; set; }
    public virtual User BookedByUserNavigation { get; set; }
    public virtual User CheckedByUserNavigation { get; set; }
    public virtual User ModifiedByUserNavigation { get; set; }
    public virtual User QuotedByUserNavigation { get; set; }
}

}

I have to return only the ConsignmentNote(but I have to pull all of them) in my controller's method.

Here is my Controller:

[Route("api/[controller]")]
[ApiController]
public class ConsignmentsController : ControllerBase
{
    private readonly WayneContext _context;

    public ConsignmentsController(WayneContext context)
    {
        _context = context;
    }

    //GET: api/Consignment Notes
    [Route("[connote]")]
    [HttpGet]
    public async Task<ActionResult<IEnumerable<Consignment>>> GetConsignmentNote()
    {
        var connote = await _context.Consignment
            .Include(x => x.ConsignmentNote)
            .ToListAsync();
        return connote;
    }
    // GET: api/Consignments
    [HttpGet]
    public async Task<ActionResult<IEnumerable<Consignment>>> GetConsignment()
    {
        var consignments = await _context.Consignment
            .Where(c => c.Deleted == false)
            .Include(bu=> bu.BookedByUserNavigation)
            .ToListAsync();
        return consignments; 
    }
}

I have to return all the connotes with this method public async Task>> GetConsignmentNote(), if you check the linq query inside the method, that query is returning exception. And also how do I override the [HttpGet] in this case?


Solution

  • Include is used to include related properties (ie your navigation properties, such as Account) and not to select a subset of normal properties like ConsignmentNote.

    If all you want is the ConsignmentNote property then you should Select a new Consignment and only populate that specific property:

    var connote = await _context.Consignment
                                .Select(x => new Consignment 
                                 { 
                                    ConsignmentNote = x.ConsignmentNote
                                 })
                                .ToListAsync();
    return connote; // List<Consignment>
    

    But note that this is still going to select a mostly empty object. If all you want is a list containing the string values only, then you could achieve that by selecting the property directly and changing the return type:

    public async Task<ActionResult<IEnumerable<string>>> GetConsignmentNote() 
    {
        var connote = await _context.Consignment
                                    .Select(x =>  x.ConsignmentNote)
                                    .ToListAsync();
        return connote; // List<string>
    }