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

时间:2017-07-26 14:14:50

标签: c# arraylist datatable count

我有一个包含3列的数据表,如下所示

                DataTable CheckFingerQty = new DataTable();
                CheckFingerQty.Columns.Add("IDp",typeof(string));
                CheckFingerQty.Columns.Add("Date", typeof(string));
                CheckFingerQty.Columns.Add("Nobat", typeof(string));
                for (int i = 0; i < arrn.Count; i++)
                {
                    string[]a= arrn[i].ToString().Split(';');
                    string []aa =a[1].Split(' ');
                    CheckFingerQty.Rows.Add(a[0],aa[0],a[2]);
                }

我从一个arraylist中读取并在此添加行。之后我想将这个数据表与IDP和Date组合在一起所以我使用下面的代码,但它对我不起作用,arrn是一个带有以下数据的arraylist

"1;04/03/2017 12:25:03;first"
"1;04/03/2017 12:25:03;first"
"1;04/03/2017 12:25:03;first"
"1;04/03/2017 12:25:03;first"
"1;04/03/2017 12:25:03;second"
"1;04/03/2017 12:25:03;second"
"1;04/03/2017 12:25:03;second"
"1;04/03/2017 12:25:03;third"
"1;04/03/2017 12:25:03;third"
"1;04/03/2017 12:25:03;third"
"2;04/03/2017 12:25:03;first"
"2;04/03/2017 12:25:03;first"
"2;04/03/2017 12:25:03;first"
"2;04/03/2017 12:25:03;first"
"2;04/03/2017 12:25:03;first"




 List<KeyValuePair<string, string[]>> CheckFingerQtyArray = new 
 List<KeyValuePair<string, string[]>>();

 CheckFingerQtyArray = CheckFingerQty.AsEnumerable()
           .Select(Row => Row["IDp"]).Distinct()
           .Select(Id => new KeyValuePair<string, string[]>(
               Id.ToString(),
               CheckFingerQty.AsEnumerable()
                   .Where(Row => Row["IDp"].ToString() == Id.ToString())
                   // .Select(Row => Row["date"].ToString() + ";" + Row["day"].ToString() + ";" + Row["nobatkari"].ToString() + ";" + Row["pname"].ToString())
                   .Select(Row => Row["Date"].ToString() + ";" + Row["Nobat"].ToString())
                   .ToArray()))
           .ToList();




 1:
   "04/03/2017;first"
   "04/03/2017;first" 
   "04/03/2017;first"
   "04/03/2017;first" 
  1:
   "04/03/2017;second"
   "04/03/2017;second" 
   "04/03/2017;second"

  1: 
   "04/03/2017;third"
   "04/03/2017;third" 
   "04/03/2017;third"

2:
   "04/03/2017;first"
   "04/03/2017;first" 
   "04/03/2017;first"
   "04/03/2017;first" 
   "05/03/2017;first"
   "05/03/2017;first" 
 2:
   "04/03/2017;second"
   "04/03/2017;second" 
   "04/03/2017;second"
   "04/03/2017;second" 


3:
   "04/03/2017;first"
   "04/03/2017;first" 
   "04/03/2017;first"
   "04/03/2017;first" 

这个输出如下所示

我想知道每个IDp和Date的第一和第二个字符串的数量 例如

1 04/03/2017 the count of first is   4 
1 04/03/2017 the count of second is  3  
2 04/03/2017 the count of first is   3 
2 04/03/2017 the count of second is  4  
2 04/03/2017 second is 2 and ...

我怎么能这样做。请帮助我

1 个答案:

答案 0 :(得分:0)

请尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication68
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] arrn = {
                "1;2017-04-03 06:29:10;first",
                "1;2017-04-03 16:19:10;first",
                "1;2017-04-03 06:49:10;first",
                "1;2017-04-03 16:29:10;first",
                "1;2017-04-03 17:06:04;second",
                "1;2017-04-03 20:26:59;second",
                "1;2017-04-03 17:27:41;second",
                "1;2017-04-03 20:27:10;second",
                "1;2017-04-03 21:07:41;third",
                "1;2017-04-03 21:27:10;third",
                "1;2017-04-03 23:27:41;third",
                "1;2017-04-03 23:47:10;third",
                "2;2017-04-03 14:56:04;first",
                "2;2017-04-03 15:26:59;first",
                "2;2017-04-03 15:27:41;first",
                "2;2017-04-03 16:29:10;first",
                "2;2017-04-04 14:56:04;first" 
                            };

            DataTable CheckFingerQty = new DataTable();
            CheckFingerQty.Columns.Add("IDp", typeof(int));
            CheckFingerQty.Columns.Add("Date", typeof(DateTime));
            CheckFingerQty.Columns.Add("Nobat", typeof(string));
            foreach (string arr in arrn)
            {
                string[] a = arr.Split(';');
                CheckFingerQty.Rows.Add(new object[] {
                    int.Parse(a[0]),
                    DateTime.Parse(a[1]).Date,
                    a[2]
                });
            }

            var groups = CheckFingerQty.AsEnumerable()
                .OrderBy(x => x.Field<DateTime>("Date"))
                .GroupBy(x => x.Field<int>("IDp")).ToList();

            foreach (var group in groups)
            {
                Console.WriteLine("IDP : '{0}'", group.Key.ToString());
                foreach (var row in group)
                {
                    Console.WriteLine("Date : '{0}'; Nobat : '{1}'", row.Field<DateTime>("Date").ToString(), row.Field<string>("Nobat"));
                }
            }

            var idpDates = CheckFingerQty.AsEnumerable().GroupBy(x => new { date = x.Field<DateTime>("Date"), dp = x.Field<int>("IDp") }).ToList();
            foreach (var idpDate in idpDates)
            {
                var noBatgroups = idpDate.GroupBy(x => x.Field<string>("Nobat")).ToList();
                foreach (var noBatgroup in noBatgroups)
                {
                    string noBatString = noBatgroup.Key + " is " + noBatgroup.Count().ToString();
                    Console.WriteLine("{0} {1} the count of {2}", idpDate.Key.dp, idpDate.Key.date.ToString("MM/dd/yyyy"), noBatString);
                }
            }

            Console.ReadLine();

        }
    }


}