您上次的回答幫我解決了絕大部分問題,無奈分組統計這裡,還需要您點化一下,多謝
aaa bb 10 cc 11 dd 12 ee 13
aaa cc 9 dd 4 bb 2 ee 13
aaa cc 16 bb 9 dd 8 ee 13
bbb a1 6 a2 9 a3 8
bbb a2 7 a3 4 a1 6"
這裡面只統計aaa 出現**3**次 bbb出現**2**次 按您的寫法該如何改呢?最好是加一列統計,期待您的回復,多謝
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string data = @"aaa bb 10 cc 11 dd 12 ee 13
aaa cc 9 dd 4 bb 2 ee 13
aaa cc 16 bb 9 dd 8 ee 13
bbb a1 6 a2 9 a3 8
bbb a2 7 a3 4 a1 6";
var r1 = data.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Split(' '));
var result = r1.SelectMany(x => Enumerable.Range(0, x.Count() / 2).Select(y => new { a = x[0], b = x[y * 2 + 1], c = int.Parse(x[y * 2 + 2]) }))
.GroupBy(x => x.a)
.Select(x => new { a = x.Key + " " + r1.Where(y => y[0] == x.Key).Count() + "次", b = x.GroupBy(y => y.b).Select(y => new { b = y.Key, c = y.Sum(z => z.c) }) });
DataTable dt = new DataTable();
for (int i = 0; i < 10; i++)
dt.Columns.Add(((char)(65 + i)).ToString());
foreach (var item in result)
{
var row = dt.NewRow();
row[0] = item.a;
int j = 1;
foreach (var item1 in item.b)
{
row[j++] = item1.b;
row[j++] = item1.c;
}
dt.Rows.Add(row);
}
DataGridView dgv = new DataGridView();
this.Controls.Add(dgv);
dgv.Dock = DockStyle.Fill;
dgv.DataSource = dt;
}
}
}