1

I have a ASP MVC API Controller that returns a IEnumerable array as JSON but I do not get the key name for the array all I get is

[1]
0:  {
$id: "1"
id: 1
itemid: "Flame 19#Bag Acosta Produce Ctn A"
descrip: "Flame grape very suite on Carton Box"
createdate: "2013-11-17T06:26:50.047"
/...

I need the key for inventory like below,

inventory:[1]
0:  {
$id: "1"
id: 1
itemid: "Flame 19#Bag Acosta Produce Ctn A"
descrip: "Flame grape very suite on Carton Box"
createdate: "2013-11-17T06:26:50.047"
/...

Code

public class DtoController : ApiController
{
    private ProduceServiceContext db = new ProduceServiceContext();

    public IEnumerable<InventoryDTO> MapInventories()
    {
        return from i in db.Inventories
        select new InventoryDTO() { id = i.InventoryId, createdate = i.CreateDate ?? DateTime.MinValue, descrip = i.Descriptions,
            itemid = i.ItemDetail, gtin = i.GTIN, lastcost = i.LastCost
/...

1 Answer 1

1
return new { inventory = from i in db.Inventories
        select new InventoryDTO() { id = i.InventoryId, createdate = i.CreateDate ?? DateTime.MinValue, descrip = i.Descriptions,
            itemid = i.ItemDetail, gtin = i.GTIN, lastcost = i.LastCost };
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your replay but I got this error: Cannot implicitly convert type 'AnonymousType#1' to 'System.Collections.Generic.IEnumerable<ProduceService.Models.InventoryDTO>
Oops, my mistake. You should just make a class like pubic class InventoryData { public IEnumerable<InventoryDTO> Inventory { get; set; } } and change your method for returning this type: return new InventoryData { Inventory = ... };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.