public TradeCategoryModel SelectTradeCategoryList() { try { TradeCategoryModel tradeCategory = new TradeCategoryModel(); cnnStr.Open(); SqlCommand cmd = new SqlCommand("select tc.InitialValue, " + "tc.FinalValue, " + "c.Category, " + "s.Sector " + "from TradeCategory tc(nolock) " + "inner join Category c(nolock) on tc.Category_id = c.id " + "inner join Sector s(nolock) on tc.Sector_id = s.id", cnnStr); using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { double finalValue; var maxvalue = Double.TryParse(String.Format("{0}", reader["FinalValue"]), out finalValue); tradeCategory.TradeList.Add( new TradeCategoryModel { InitialValue = Double.Parse(String.Format("{0}", reader["InitialValue"])), FinalValue = finalValue, Category = String.Format("{0}", reader["Category"]), Sector = String.Format("{0}", reader["Sector"]) }); } return(tradeCategory); } } catch (Exception ex) { throw ex; } finally { cnnStr.Close(); } }
public List <String> SortCategoryByValue(List <TradeModel> tradeList) { try { TradeCategoryModel tradecategories = this.AllCategories(); if (tradeList.Count > 0) { List <String> catList = new List <string>(); foreach (var trade in tradeList) { var category = tradecategories.TradeList.Where(tc => tc.InitialValue < trade.Value && (tc.FinalValue > trade.Value || tc.FinalValue == 0) && tc.Sector.ToLower() == trade.ClientSector.ToLower()).ToList(); if (category.Count == 0) { throw new Exception("There is no information to evaluate the category for value - " + trade.Value); } if (category.Count() > 1) { throw new Exception("There are more than one category for the informed value - " + trade.Value); } catList.Add(category.First().Category); } return(catList); } else { throw new Exception("At least one value is required for the category to be evaluated!"); } } catch (Exception ex) { throw ex; } }