public static List <String> processData(IEnumerable <string> lines) { List <String> retVal = new List <String>(); List <DiscountProduct> discountProducts = new List <DiscountProduct>(); List <BuyersData> ConvertedData = BuyersData.GetConvertedData(lines); IEnumerable <string> DistinctProducts = ConvertedData.Select(x => x.ProductName).Distinct(); var q1 = from b in ConvertedData select b; var q2 = (from b in ConvertedData select b).ToArray(); foreach (string product in DistinctProducts) { var minPrice = ConvertedData.Where(x => x.ProductName == product).Min(p => p.Price); var maxPrice = ConvertedData.Where(x => x.ProductName == product).Max(p => p.Price); if (minPrice != maxPrice) { DiscountProduct discountProduct = new DiscountProduct(); discountProduct.ProductName = product; discountProduct.MinPrice = minPrice; discountProduct.MaxPrice = maxPrice; discountProducts.Add(discountProduct); } } foreach (DiscountProduct discountProduct in discountProducts) { var customerName = ConvertedData.FirstOrDefault(x => x.Price == discountProduct.MinPrice && x.ProductName == discountProduct.ProductName) .CustomerName; var sometimesBroughtOnHigherPrice = false; foreach (DiscountProduct innerLoop in discountProducts) { sometimesBroughtOnHigherPrice = ConvertedData.Any(x => x.CustomerName == customerName && x.ProductName == innerLoop.ProductName && x.Price == innerLoop.MaxPrice); if (sometimesBroughtOnHigherPrice) { break; } } if (!sometimesBroughtOnHigherPrice) { retVal.Add(customerName); } } return(retVal); }
public static List <BuyersData> GetConvertedData(IEnumerable <string> lines) { List <BuyersData> Data = new List <BuyersData>(); foreach (string line in lines) { string[] values = line.Split(','); BuyersData buyerValues = new BuyersData(); buyerValues.CustomerName = values[0].Trim(); buyerValues.StoreLocation = values[1].Trim(); buyerValues.DayOfMonth = Convert.ToInt16(values[2].Trim()); buyerValues.ProductName = values[3].Trim(); buyerValues.Price = Convert.ToDecimal(values[4].Replace("Rs", "").Trim()); buyerValues.PaymentType = values[5].Trim(); Data.Add(buyerValues); } return(Data); }