public List<ProductMobileModel> GetSuggestProduct(string username) { try { var productModel = (from p in context.Products from h in context.HistoryDetails where h.ProductId == p.Id && h.History.Username.Equals(username) group h by p into productMostBuyGroup select new ProductMostBuy { Product = productMostBuyGroup.Key, numberOfBuy = productMostBuyGroup.Count() }).Where(x => x.numberOfBuy >= 5).OrderByDescending(o => o.numberOfBuy).Take(5); var result = new List<ProductMobileModel>(); foreach (ProductMostBuy pmb in productModel) { var product = (from p in context.ProductAttributes where p.ProductId == pmb.Product.Id group p by p.ProductId into grp select grp.OrderByDescending(o => o.LastUpdatedTime).FirstOrDefault()).FirstOrDefault(); DateTime? lastUpdateTime = product.LastUpdatedTime; string lastUpdateTimeFormat = String.Format("{0:MM-dd-yyyy}", lastUpdateTime); var info = new ProductMobileModel { ProductId = product.ProductId.ToString(), Name = product.Product.Name, MinPrice = product.MinPrice.ToString(), MaxPrice = product.MaxPrice.ToString(), LastUpdateTime = lastUpdateTimeFormat }; result.Add(info); } return result; } catch { return null; } }
public List<ProductMobileModel> Search(string keyword) { //search product by dictionary var dictionaries = context.Dictionaries.Include(i => i.Product).Where(p => p.Name.Contains(keyword)).OrderBy(p => p.Name).ToList(); var result = new List<ProductMobileModel>(); foreach (Dictionary dictionary in dictionaries) { List<int?> minPrice = dictionary.Product.ProductAttributes .OrderByDescending(x => x.LastUpdatedTime) .Select(x => x.MinPrice) .ToList(); List<int?> maxPrice = dictionary.Product.ProductAttributes .OrderByDescending(x => x.LastUpdatedTime) .Select(x => x.MaxPrice) .ToList(); var productName = context.Products.Where(p => p.Id == dictionary.ProductId).Select(p => p.Name).FirstOrDefault(); if (!result.Any(p => p.Name == productName)) { var info = new ProductMobileModel { ProductId = dictionary.ProductId.ToString(), Name = productName, MinPrice = minPrice[0].ToString(), MaxPrice = maxPrice[0].ToString() }; result.Add(info); } } // result.OrderBy(p => p.Name); return result; }
public List<ProductMobileModel> GetProduct() { var products = context.Products.Include(i => i.ProductAttributes).ToList(); var result = new List<ProductMobileModel>(); foreach (Product product in products) { List<int?> minPrice = product.ProductAttributes .OrderByDescending(x => x.LastUpdatedTime) .Select(x => x.MinPrice) .ToList(); List<int?> maxPrice = product.ProductAttributes .OrderByDescending(x => x.LastUpdatedTime) .Select(x => x.MaxPrice) .ToList(); DateTime? lastUpdateTime = product.ProductAttributes .OrderByDescending(x => x.LastUpdatedTime) .Select(x => x.LastUpdatedTime).FirstOrDefault(); string lastUpdateTimeFormat = String.Format("{0:MM-dd-yyyy}", lastUpdateTime); var proAtt = context.ProductAttributes.Where(p => p.ProductId == product.Id).OrderByDescending(x => x.LastUpdatedTime).FirstOrDefault(); if (proAtt != null) { var info = new ProductMobileModel { ProductId = product.Id.ToString(), Name = product.Name, MinPrice = minPrice[0].ToString(), MaxPrice = maxPrice[0].ToString(), LastUpdateTime = lastUpdateTimeFormat }; result.Add(info); } } return result; }