Subquery LINQ in WebApi2 controller RRS feed

  • Question

  • User-1473047469 posted
    Im working with webapi2 controller , this is the code i have , and i want to retrieve child of child elements :How to do this subquery here :

    [HttpGet]        [Route("api/Home/GetHomeMenuKategori/{id}")]        // GET: api/Kafiqs1/5        [ResponseType(typeof(
    Home))]        public IHttpActionResult GetKafiqMenuKategori(string id)        {                     var x = db.Homes.Where(b => b.HomeID == id).Select(b => b.MenuCategories).Select(                                b=> db.MenuCategories.Include(c => c.Items)).ToList();            // Home has an Icollection of Menucategories            //Menucategories has an Icollection of Items            //I want home to include menucategories and then menucategories to include             if (x == null)            {                return NotFound();            }            return Ok(x);        }

    http://localhost:59381/api/Home/GetHomeMenuKategori/HOME1  returns this , from this action:

         // GET: api/Kafiqs1/5
         public IHttpActionResult GetKafiqMenuKategori(string id)
             var x = db.Kafiqs.Where(b => b.KafiqID == id).Select(b => b.MenuCategories).ToList();
             if (x == null)
                 return NotFound();
             return Ok(x);

                "$id": "1",
                "MenuCategoryID": "KAFE",
                "HomeID": "Home2",
                "Image": "KAFE",
                "Name": "KAFE",
                "Home": null,
                "Items": [] // there should be listed the menu items 
                "$id": "2",
                "MenuCategoryID": "MENUKATEGORI2",
                "HomeID": "Home2",
                "Image": "MENUKATEGORI2",
                "Name": "MENUKATEGORI2",
                "Home": null,
                "Items": []

    Friday, January 19, 2018 10:02 AM

All replies

  • User283571144 posted

    Hi mileycyrus,

    Subquery LINQ in WebApi2 controller

    According to your codes and description, I couldn't create a test demo on my side.

    I suggest you could try to provide the model class like MenuCategories, home and so on.

    If you could post more details information, it will be more easily for us to reproduce the issue and find the solution.

    Best Regards,


    Monday, January 22, 2018 9:11 AM