1

I want to get a nested JSON reply with my EF query and not sure how to do this. I have declared my Models as follows:

  [Serializable]
    public class StockReturnMethod
    {
        public int WarehouseID { get; set; }
        public int ProductSKUID { get; set; }
        public int LotID { get; set; }
        public string LotName { get; set; }
        public int AreaID { get; set; }
        public string AreaName { get; set; }
        public int BinID { get; set; }
        public string BinName { get; set; }
        public List<AvailibleStock> Stock { get; set; }
    }


  [Serializable]
    public class AvailibleStock
    {
        public int WarehouseID { get; set; }
        public int ProductSKUID { get; set; }
        public string ProductSKUName { get; set; }
        public string WarehouseName { get; set; }
        public string Status { get; set; }
        public int QtyUnassigned { get; set; }
    }

Here is my EF query that I have so far.

    {
       return (from WH in SCMENT.Warehouses 
               join WL in SCMENT.WarehouseLots on WH.WarehouseID equals WL.WarehouseID
               join WA in SCMENT.WarehouseAreas on WL.WarehouseLotID equals WA.WarehouseLotID
               join WB in SCMENT.WarehouseBins on WA.WarehouseAreaID equals WB.WarehouseAreaID
               join SLI in SCMENT.StockLineItems on WH.WarehouseID equals SLI.WarehouseID
               join PSKU in SCMENT.ProductSKUs on SLI.ProductSKUID equals PSKU.ProductSKUID
               where SLI.SystemAreaID == 1

               select new StockReturnMethod() 
               {
                WarehouseID = WH.WarehouseID,
                LotID = WL.WarehouseLotID,
                LotName = WL.WarehouseLotName,
                AreaID = WA.WarehouseAreaID,
                AreaName = WA.WarehouseAreaName,
                BinID = WB.WarehouseBinID,
                BinName = WB.WarehouseBinName,
                ProductSKUID = PSKU.ProductSKUID,
                Stock = (Will I create a sub query here?)


               }

    )

1 Answer 1

2
public List<AvailibleStock> Stock { get; set; }

Change this to:

public IList<AvailibleStock> Stock { get; set; }

Edit: Here's working sample from one of my projects that you can use as reference:

public class StatisticsModel
{
    public int Id { get; set; }
    public int ClientId { get; set; }
    public int Opened { get; set; }
    public IEnumerable<LocationsModel> Locations { get; set; }
}


public class LocationModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int OpenCount { get; set; }
}


return dbSet.Select(x => new StatisticsModel
{
    Id = x.Id,
    ClientId = x.ClientId,
    Opened = x.OpenCount,
    Locations = x.Locations.Select(z => new LocationsModel{
        Id = z.Id,
        Name = z.Store.Name,
        OpenCount = z.OpenCount
    })
})
Sign up to request clarification or add additional context in comments.

3 Comments

Added the suggested changes, i get ****Invalid initializer member declarator**** with this line of code: SLI.select(z => new AvailibleStock
Since I don't have insight in your data tables, I've provided you with one of my models and projections. I believe you will be able to extract the logic.
You Sir Are a gentlemen and a scholar!

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.