0
votes

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?

2

2 Answers

1
votes

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>
}
0
votes

Hi What if you try something like this:

//[Route("[connote]")] < --remove route and call your method GET to verride httpget .. it will match the api/Consignments [GET]
    //[HttpGet]
    public async Task<ActionResult<IEnumerable<ConsignmentNote>>> Get()
    {
        var connote = await _context.Consignment
            .Include(x => x.ConsignmentNote)
            .ToListAsync();
        return connote.Select(xx=>xx.ConsignmentNote).ToList();
    }

Hope it helps you!!