Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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"

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
Posted
Updated 8-May-17 5:59am
v2
Comments
deepankarbhatnagar 8-May-17 11:27am    
Please elaborate your query.

You should use List<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;}
}


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:
C#
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]
        });
}


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 
 
Share this answer
 
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:
C#
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];
                }
            }
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900