private List <GrupoViewModel> RecuperacionGrupos(string id) { if (!string.IsNullOrWhiteSpace(id) && !GroupsXml.Any(p => p.GroupID == id)) { return(null); } DateTime inicio = DateTime.Now; using (InventarioEntities db = new InventarioEntities()) { string user = User.Identity.GetUserName(); var enCapsulas = db.CapsulasItems .Where(s => s.Capsulas.IdUsuario == user) .GroupBy(t => t.ItemID) .Select(u => new { ItemID = u.Key, CantidadCapsula = u.Sum(v => v.Cantidad) }).ToList(); var inventario = db.Inventarios .Where(s => s.IdUsuario == user) .Select(u => new { ItemID = u.ItemID, Cantidad = u.Cantidad }).ToList(); var grupos = GroupsXml.Where(z => string.IsNullOrWhiteSpace(id) || z.GroupID == id) .Select(p => new GrupoViewModel { GroupID = p.GroupID, Tipos = p.Types.Select(q => new TipoViewModel { TypeID = q.TypeID, Items = q.Items.Select(r => new ItemInventoryViewModel { CurrentItem = r, Cantidad = inventario.SingleOrDefault(s => s.ItemID == r.ItemID) == null ? 0 : inventario.Single(s => s.ItemID == r.ItemID).Cantidad, CantidadCapsulas = enCapsulas.SingleOrDefault(s => s.ItemID == r.ItemID) == null ? 0 : enCapsulas.Single(s => s.ItemID == r.ItemID).CantidadCapsula }) }) }); double elapsed = (DateTime.Now - inicio).TotalMilliseconds; var lista = new List <GrupoViewModel>(); if (!string.IsNullOrWhiteSpace(id)) { var model = grupos.Single(); model.Total = model.Tipos.SelectMany(q => q.Items).Sum(r => r.Cantidad); model.TotalCapsulas = model.Tipos.SelectMany(q => q.Items).Sum(r => r.CantidadCapsulas); lista.Add(model); } else { lista = grupos.ToList(); } return(lista); } }
public ActionResult Edit(string id) { if (string.IsNullOrWhiteSpace(id)) { return(new HttpNotFoundResult()); } if (!GroupsXml.Any(p => p.GroupID == id)) { return(new HttpNotFoundResult()); } using (InventarioEntities db = new InventarioEntities()) { string user = User.Identity.GetUserName(); var enCapsulas = db.CapsulasItems .Where(s => s.Capsulas.IdUsuario == user) .GroupBy(t => t.ItemID) .Select(u => new { ItemID = u.Key, CantidadCapsula = u.Sum(v => v.Cantidad) }).ToList(); var inventario = db.Inventarios .Where(s => s.IdUsuario == user) .Select(u => new { ItemID = u.ItemID, Cantidad = u.Cantidad }).ToList(); var grupo = GroupsXml.Where(z => z.GroupID == id) .Select(p => new { GroupID = p.GroupID, Items = p.Types.SelectMany(z => z.Items).Select(r => new ItemEditViewModel { CurrentItem = r, Cantidad = inventario.SingleOrDefault(s => s.ItemID == r.ItemID) == null ? 0 : inventario.Single(s => s.ItemID == r.ItemID).Cantidad, CantidadCapsulas = enCapsulas.SingleOrDefault(s => s.ItemID == r.ItemID) == null ? 0 : enCapsulas.Single(s => s.ItemID == r.ItemID).CantidadCapsula }) }).Single(); GrupoEditViewModel model = new GrupoEditViewModel(); model.GroupID = grupo.GroupID; model.Items = grupo.Items.ToList(); model.Total = model.Items.Sum(r => r.Cantidad); model.TotalCapsulas = model.Items.Sum(r => r.CantidadCapsulas); return(View(model)); } }