Store data within alphabetically ordered letters (A - D, E - H, I - L, etc) using Dictionary
-
I'm using entity framework and I'd like to store the total count of items within the bounds of a specific set of letters (if this makes sense): A - D (12) - (where 12 is the total amount and the data falls between letters A and D). E - H (24) I - L (9) M - P (62) Q - T (18) U - Z (0) I wasn't quite sure how to do this with a Dictionary. Let's say my model object consists of an Id and Title and that it's called Discount. At the moment, I have the following: List<Discount> discounts = new List<Discount>(); Dictionary<string, Discount> dict = new Dictionary<string, Discount>(); foreach (DiscountDTO discount in discounts) { if (discount.Title.StartsWith("A") || discount.Title.StartsWith("B") || discount.Title.StartsWith("C") || discount.Title.StartsWith("D")) { dict.Add("A - D", discount); } else if (discount.Title.StartsWith("E") || discount.Title.StartsWith("F") || discount.Title.StartsWith("G") || discount.Title.StartsWith("H")) { dict.Add("E - H", discount); } else if (discount.Title.StartsWith("I") || discount.Title.StartsWith("J") || discount.Title.StartsWith("K") || discount.Title.StartsWith("L")) { dict.Add("I - L", discount); } else if (discount.Title.StartsWith("M") || discount.Title.StartsWith("N") || discount.Title.StartsWith("O") || discount.Title.StartsWith("P")) { dict.Add("M - P", discount); } else if (discount.Title.StartsWith("Q") || discount.Title.StartsWith("R") || discount.Title.StartsWith("S") || discount.Title.StartsWith("T")) { dict.Add("Q - T", discount); } else if (discount.Title.StartsWith("U") || discount.Title.StartsWith("V") || discount.Title.StartsWith("W") || discount.Title.StartsWith("X") || discount.Title.StartsWith("Y") || discount.Title.StartsWith("Z")) { dict.Add("U - Z", discount); } } Thanks in advance.
-
Answer:
you should use a List as the value of the dictionary: static void Main(string[] args) { List<Discount> list = new List<Discount>(); list.Add(new Discount { Id = 1, Title = "Adam" }); list.Add(new Discount { Id = 2, Title = "Ben" }); list.Add(new Discount { Id = 3, Title = "Alex" }); list.Add(new Discount { Id = 4, Title = "Daniel" }); list.Add(new Discount { Id = 5, Title = "Ethan" }); list.Add(new Discount { Id = 6, Title = "Howard" }); list.Add(new Discount { Id = 7, Title = "Peter" }); list.Add(new Discount { Id = 8, Title = "Tazz" }); list.Add(new Discount { Id = 9, Title = "Steve" }); list.Add(new Discount { Id = 10, Title = "Lyle" }); Dictionary<string, List<Discount>> dic = new Dictionary<string, List<Discount>>(); foreach (Discount d in list) { string range = GetRange(d.Title); if (dic.ContainsKey(range)) dic[range].Add(d); else dic.Add(range, new List<Discount> { d }); } } static string GetRange(string s) { char c = s.ToLower()[0]; if (c >= 'a' && c <= 'd') return "A - D"; else if (c >= 'e' && c <= 'h') return "E - H"; else if (c >= 'i' && c <= 'l') return "I - L"; else if (c >= 'm' && c <= 'p') return "M - P"; else if (c >= 'q' && c <= 't') return "Q - T"; else if (c >= 'u' && c <= 'z') return "U - Z"; return ""; }
gotnull at Stack Overflow Visit the source
Other answers
is this what you want: static void Main(string[] args) { List<Discount> list = new List<Discount>(); list.Add(new Discount { Id = 1, Title = "Adam" }); list.Add(new Discount { Id = 2, Title = "Ben" }); list.Add(new Discount { Id = 3, Title = "Alex" }); list.Add(new Discount { Id = 4, Title = "Daniel" }); list.Add(new Discount { Id = 5, Title = "Ethan" }); list.Add(new Discount { Id = 6, Title = "Howard" }); list.Add(new Discount { Id = 7, Title = "Peter" }); list.Add(new Discount { Id = 8, Title = "Tazz" }); list.Add(new Discount { Id = 9, Title = "Steve" }); list.Add(new Discount { Id = 10, Title = "Lyle" }); var query = list.GroupBy(d => GetRange(d.Title)); foreach (var group in query) Console.WriteLine("{0}[{1}]", group.Key, group.Count()); } static string GetRange(string s) { char c = s.ToLower()[0]; if (c >= 'a' && c <= 'd') return "A - D"; else if (c >= 'e' && c <= 'h') return "E - H"; else if (c >= 'i' && c <= 'l') return "I - L"; else if (c >= 'm' && c <= 'p') return "M - P"; else if (c >= 'q' && c <= 't') return "Q - T"; else if (c >= 'u' && c <= 'z') return "U - Z"; return ""; }
ojlovecd
Related Q & A:
- Can I set up my e-mail account in Microsoft outlook even if I have a free e-mail account with yahoo.ca?Best solution by Yahoo! Answers
- What to do if I Got a D in a freshman year?Best solution by Yahoo! Answers
- How to get a G.E.D online?Best solution by eHow old
- Can I get SSI for A.D.D?Best solution by answers.yahoo.com
- How can one obtain a G.E.D?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.