2017-07-26 79 views
0

我有3列数据表如下如何计算在C#.NET数组列表中的项目串

   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读取并添加行到this.after,我想这GROUPBY与数据表IDP和日期,以便我用下面的代码,但它不为我工作arrn与下面的数据

"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" 

的这个输出是像下面

我想知道〜应变的量的数组列表GS第一和各IDP的第二次和日期 例如

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 ... 

我该怎么办this.please帮我

回答

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(); 

     } 
    } 


} 
+0

我看起来像所有这些都是需要的是truncate date to midnight:CheckFingerQty.Rows.Add(new object [] { int.Parse(a [0]), DateTime.Parse(a [1])。Date, a [2] }); – jdweng

+0

什么不起作用? – jdweng

+0

请检查有问题的例子。我想输出是这样的1 04/03/2017首先是4 1 04/03/2017第二的数字是3 2 04/03/2017计数的第一个是3 2 04/03/2017秒的计数是4现在你的代码只计数每个IDP.bot第一,第二和第三为每个IDp – amin

相关问题