如何在c#.net中按arraylist分组 [英] How to group by arraylist in c#.net
问题描述
我有一个带有以下数据的arrayList作为一个字符串,它被分隔;在c#.net中我想计算特殊日期时间内每个ID的类型我该怎么做
I have a arrayList with below data as a string which is seperated by ; in c#.net.I want to count type for each ID in a special datetime How can I do this
ArrayList arrn = new ArrayList();
"1;2017-2-3 08:21:03;type1"
"1;2017-2-4 08:21:03;type1"
"1;2017-2-4 08:21:03;type1"
"2;2017-2-3 08:21:03;type1"
"2;2017-2-3 08:21:03;type1"
"3;2017-2-3 08:21:03;type1"
请帮我分组这个arraylist来计算类型在每个ID的特殊日期时间
我尝试过:
我在互联网上搜索了很多但是我没有成功
please help me to group by this arraylist to count types in a special datetime for each ID
What I have tried:
I searched a lot in internet but I was not successful
推荐答案
你应该使用List< T>
代替 ArrayList [ ^ ]。
为了能够按id分组数据,你必须创建自定义对象,例如:
You should useList<T>
instead of ArrayList[^].
To be able to group data by id, you have to create custom object, for example:
public class MyObject
{
public int Id {get; set;}
public DateTime Dt {get; set;}
public string sType {get; set;}
}
现在,您必须创建 List< MyObject>
,能够通过 arrn
对象的项目并将它们转换为 MyObject的列表
:
Now, you have to create List<MyObject>
, to be able to go though the items of arrn
object and convert them into a List of MyObject
:
List<MyObject> mydata = new List<MyObject>();
foreach(string x in arrn)
{
mydata.Add(new MyObject
{
Id = Convert.ToInt32(x.Split(new string[]{";"}, StringSplitOptions.RemoveEmptyEntries)[0]),
Dt = Convert.ToDateTime(x.Split(new string[]{";"}, StringSplitOptions.RemoveEmptyEntries)[1]),
sType = x.Split(new string[]{";"}, StringSplitOptions.RemoveEmptyEntries)[2]
});
}
最后,您已准备好使用 Linq [ ^ ]对数据进行分组和统计:
Finally, you're ready to use Linq[^] to group and count data:
var result = mydata
.GroupBy(x=>x.Id)
.Select(grp=> new
{
Id = grp.Key,
Count = grp.Count()
})
.ToList();
结果:
Result:
Id Count
1 3
2 2
3 1
最好的办法是:
1)停止使用ArrayList - 它被泛型集合取代在.NET的.NET中!
2)创建一个类来保存值,并给它一个带字符串的构造函数。
首先,使用string拆分每个字符串.Split:
然后使用DateTime.TryParseExact将每个日期转换为DateTime值,并使用intTryParse转换ID:
The best things to do are:
1) Stop using ArrayList - it was superseded by generic collections in V2 of .NET!
2) Create a class to hold the values, and give it a constructor which takes a string.
First, split each string using string.Split:
Then use DateTime.TryParseExact to convert each date to a DateTime value, and intTryParse to convert the ID:
private class MyData
{
private int _ID;
public int ID
{
get { return _ID; }
set { _ID = value; }
}
private DateTime _Date;
public DateTime Date
{
get { return _Date; }
set { _Date = value; }
}
private string _Type;
public string Type
{
get { return _Type; }
set { _Type = value; }
}
public MyData(string s)
{
string[] parts = s.Split(';');
if (parts.Length != 3) throw new ArgumentException("Not a valid MyData element: " + s);
if (!int.TryParse(parts[0], out _ID)) throw new ArgumentException("Bad ID Value: " + parts[0]);
if (!DateTime.TryParseExact(parts[1], "yyyy-M-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out _Date)) throw new ArgumentException("Bad date format: " + parts[1]);
_Type = parts[2];
}
}
使用每个字符串生成一个新的MyData实例,并将它们存储在List< MyData>
然后它是非常简单的使用Linq查询(甚至是foreach循环)来获取按日期,ID或两者分组的总计。
Use each string to generate a new instance of MyData, and store them in a List<MyData>
Then it's pretty trivial to use a Linq query (or even a foreach loop) to get the totals grouped by date, ID, or both.
这篇关于如何在c#.net中按arraylist分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!