Create JSON object from self-nested LINQ query




public async Task<IHttpActionResult> GetItemsForFormType()
{
    var result = new List<ItemManagementViewModel>();

    using (var db = new Context())
    {
        result = await (from x1 in db.Values
                        join x2 in db.Values on x1.ParentId equals x2.RecordId
                        join x3 in db.Values on x1.RecordId equals x3.ParentId
                        orderby x1.RecordId
                        group x2 by x2.Name into gx
                        select
                            new ItemManagementViewModel
                                {
                                    FormType = gx.Key,
                                    FormControllers = (from y1 in db.Values
                                                      join y2 in db.Values on y1.ParentId equals y2.RecordId
                                                      join y3 in db.Values on y1.RecordId equals y3.ParentId
                                                      orderby y1.RecordId
                                                      where y2.Name == gx.Key
                                                      group new {y1,y2,y3} by y1.RecordId into gy                                                           
                                                      select new FormControllerViewModel
                                                                {
                                                                    FormControllerID = gy.Key,
                                                                    FormControllerName = (from c in gy select c.y1.Name).FirstOrDefault(),
                                                                    FormControllerValues = from z1 in db.Values
                                                                                           join z2 in db.Values on z1.ParentId equals z2.RecordId
                                                                                           join z3 in db.Values on z1.RecordId equals z3.ParentId
                                                                                           orderby z1.RecordId
                                                                                           where z1.RecordId == gy.Key
                                                                                           group new  {z3}  by z3.RecordId into gz
                                                                                           select new FormControllerValueViewModel
                                                                                                      {
                                                                                                          FormControllerValueID = gz.Key,
                                                                                                          FormControllerValueName = (from v in gz select v.z3.Name).FirstOrDefault(),
                                                                                                          Deleted = (from v in gz select v.z3.Deleted).FirstOrDefault(),
                                                                                                          ModifiedOn = (from v in gz select v.z3.ModifiedOn).FirstOrDefault(),
                                                                                                          ModifiedBy = (from v in gz select v.z3.ModifiedBy).FirstOrDefault(),
                                                                                                          CreatedBy = (from v in gz select v.z3.CreatedBy).FirstOrDefault(),
                                                                                                          CreatedOn = (from v in gz select v.z3.CreatedOn).FirstOrDefault(),
                                                                                                      }

                                                                })
                                }).ToListAsync();
        if (result == null)
        {
            return this.NotFound();
        }
        return this.Ok(result);
    }
}   

Search This Blog

Arsip Blog

Powered by Blogger.

Recent

Comment

Author Info

Like This Theme

Popular Posts

Video Of Day

Sponsor

Most Popular