使用字典按字母顺序存储数据(A - D, E - H, I - L等)
本文关键字:字典 数据 顺序存储 | 更新日期: 2023-09-27 18:11:43
我正在使用实体框架,我想在一组特定的字母范围内存储项目的总数(如果这有意义的话):
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)
我不太确定如何使用Dictionary
。
假设我的模型对象由Id
和Title
组成,它被称为Discount
。
目前,我有以下内容:
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);
}
}
这是你想要的吗:
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 "";
}
你应该使用List作为字典的值:
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 "";
}