Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c#.net计算arraylist中的字符串_C#_String_Arraylist_Count - Fatal编程技术网

如何使用c#.net计算arraylist中的字符串

如何使用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-

我有一个数组列表,其中的字符串用“;”分隔所以我的问题是,如何按字符串中的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-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})