如何使用c#.net计算arraylist中的字符串
我有一个数组列表,其中的字符串用“;”分隔所以我的问题是,如何按字符串中的ID和日期计算其中的“type”元素如何使用c#.net计算arraylist中的字符串,c#,string,arraylist,count,C#,String,Arraylist,Count,我有一个数组列表,其中的字符串用“;”分隔所以我的问题是,如何按字符串中的ID和日期计算其中的“type”元素 ArrayList arrn=new ArrayList (); arrn.Add(item.Key + ";" + datetime.ToString() + ";type1"); "1;2017-5-3 20:36:41;type1" "1;2017-5-3 20:36:41;type1" "3;2017-5-3 20:36:41;type2" "3;2017-5-
ArrayList arrn=new ArrayList ();
arrn.Add(item.Key + ";" + datetime.ToString() + ";type1");
"1;2017-5-3 20:36:41;type1"
"1;2017-5-3 20:36:41;type1"
"3;2017-5-3 20:36:41;type2"
"3;2017-5-3 20:36:41;type2"
"2;2017-5-3 20:36:41;type3"
"2;2017-5-3 20:36:41;type3"
"1;2017-5-4 20:36:41;type2"
"1;2017-5-4 20:36:41;type2"
"4;2017-5-3 20:36:41;type1"
"4;2017-5-3 20:36:41;type1"
我想要下面的输出
1 2017-5-3 : count type1 :2
1 2017-5-4 : count type2 :2
2 2017-5-3 : count type3 :2
3 2017-5-3 : count type2 :2
4 2017-5-3 : count type1 :2
我该怎么做请帮我你可以
IDictionary<string, IDictionary<string, IDictionary<string, int>>>
IDictionary
定义用于跟踪计数器的结构
在“;”上拆分字符串,并迭代标记,并通过测试键的存在性将它们添加到上述结构中。如果您是第一次添加一个键,不要忘记为Dictionary对象设置默认值,或者将计数器设置为1/0
var arrn = new ArrayList{
"1;2017-5-3 20:36:41;type1",
"1;2017-5-3 20:36:41;type1",
"3;2017-5-3 20:36:41;type2",
"3;2017-5-3 20:36:41;type2",
"2;2017-5-3 20:36:41;type3",
"2;2017-5-3 20:36:41;type3",
"1;2017-5-4 20:36:41;type2",
"1;2017-5-4 20:36:41;type2",
"4;2017-5-3 20:36:41;type1",
"4;2017-5-3 20:36:41;type1"};
arrn.ToArray()
.GroupBy(o => string.Join(" ", ((string) o).Split(';', ' ').Take(2)))
.Select(objects => $"{objects.Key} : count :{objects.Count()}");
如果你想要一个漂亮的解决方案,想想你自己。古鲁拉指出,考虑使用字典,让自己了解这些应该会有所帮助。
然而,对我来说,使用ArrayList的整个尝试似乎很奇怪——但我不知道您是从API还是类似的东西获得ArrayList,所以我不建议使用其他类型
如果您在做了一些研究并尝试了一些东西之后,有任何以前的特定问题,请随时提问。到目前为止您尝试过什么吗?首先,您需要解析代码将这些字符串转换为具有单独值的内容(可能解析为
DateTime
)。然后Linq将类似于var results=parsed.GroupBy(p=>new{p.Id,p.TimeStamp.Date,p.Type})亲爱的朱哈尔:请给我发一些代码,你在代码项目板上问过。我想让你试试看。它应该非常简单,就像foreach(arrn中的var-item){var-tokens=item.Split(“;”);foreach(令牌中的var-token){//在这里添加逻辑}}亲爱的Gururaj:我如何根据类型、日期时间和id按此arraylist分组