public ActionResult Detail(int id) { var person = db.People.Find(id); //Setup detailedPerson with personal info PersonDashboardVM detailedPerson = new PersonDashboardVM { //personal info FullName = person.FullName, PreferredName = person.PreferredName, PhoneNumber = person.PhoneNumber, FaxNumber = person.FaxNumber, EmailAddress = person.EmailAddress, ValidFrom = person.ValidFrom, Photo = person.Photo }; //check if customers2 is null and return if so if (person.Customers2.FirstOrDefault() == null) { return(View(detailedPerson)); } //Fill in detailedPerson with more information if matches customer2 id if (person.Customers2.FirstOrDefault().PrimaryContactPersonID == id) { //company info var customer = db.People.Find(id).Customers2.FirstOrDefault(); detailedPerson.CompanyName = customer.CustomerName; detailedPerson.CompanyPhoneNumber = customer.PhoneNumber; detailedPerson.CompanyFaxNumber = customer.PhoneNumber; detailedPerson.Website = customer.WebsiteURL; detailedPerson.YearStarted = customer.ValidFrom.Year; //purchases info detailedPerson.TotalOrders = customer.Orders.Count(); detailedPerson.GrossSales = customer.Orders.Sum(n => n.Invoices.Sum(n2 => n2.InvoiceLines.Sum(n3 => n3.ExtendedPrice))); detailedPerson.GrossProfit = customer.Orders.Sum(n => n.Invoices.Sum(n2 => n2.InvoiceLines.Sum(n3 => n3.LineProfit))); //make list of top 10 purchases var topPurchases = customer.Orders.SelectMany(n => n.Invoices.SelectMany(n2 => n2.InvoiceLines.Select(n3 => new { StockItemID = n3.StockItemID, LineProfit = n3.LineProfit, Description = n3.Description, SP = n3.Invoice.Person4.FullName }))).OrderByDescending(n4 => n4.LineProfit).Take(10).ToList(); detailedPerson.MostProfitableItems = new List <ItemPurchased>(); //populate detailedPersons mostProfitable Item list with topPurchases for (int i = 0; i < topPurchases.Count(); i++) { detailedPerson.MostProfitableItems.Add(new ItemPurchased(topPurchases.ElementAt(i).StockItemID, topPurchases.ElementAt(i).Description, topPurchases.ElementAt(i).LineProfit, topPurchases.ElementAt(i).SP)); } return(View(detailedPerson)); } return(View(detailedPerson)); }
public ActionResult Index(PersonDashboardVM model) { model.PersonResults = db.People .Where(temp => temp.FullName.Contains(model.Name)) .Select(p => new PersonSearchVM { Name = p.FullName, Alias = p.PreferredName, } ).ToList(); db.Dispose(); return(View(model)); }