2 audiohifi audiohifi 于 2016.02.29 19:24 提问

c# 数据统计 请问如何实现
c#
遇到一个棘手问题,请老师们帮解答一下:
    原始的表
    ![图片说明](http://img.ask.csdn.net/upload/201602/29/1456744918_718502.jpg)

    统计表
    ![图片说明](http://img.ask.csdn.net/upload/201602/29/1456744972_848207.jp

    请问用c#如何实现

7个回答

caozhy
caozhy   Ds   Rxr 2016.02.29 20:44
已采纳
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Q239660
{
    class Program
    {
        static void Main(string[] args)
        {
            string data = @"aaa bb 10 cc 11 dd 12
aaa cc 9 dd 4 bb 2
aaa cc 16 bb 9 dd 8
bbb a1 6 a2 9 a3 8
bbb a2 7 a3 4 a1 6";
            string result = string.Join("\r\n", data.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
                .Select(x => x.Split(' ')).SelectMany(x => Enumerable.Range(0, 3).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, b = x.GroupBy(y => y.b).Select(y => new { b = y.Key, c = y.Sum(z => z.c) }) })
                .Select(x => x.a + " " + string.Join(" ", x.b.Select(y => y.b + " " + y.c.ToString()))));
            Console.WriteLine(result);
        }
    }
}

caozhy
caozhy   Ds   Rxr 2016.03.03 03:18
 using System;
using System.Collections.Generic;
using System.Linq;

public class Test
{
    public static void Main()
    {
            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";
            string result = string.Join("\r\n", data.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries)
                .Select(x => x.Split(' ')).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 + " " + x.Count() + "次", b = x.GroupBy(y => y.b).Select(y => new { b = y.Key, c = y.Sum(z => z.c) }) })
                .Select(x => x.a + " " + string.Join(" ", x.b.Select(y => y.b + " " + y.c.ToString()))));
            Console.WriteLine(result);
    }
}

http://ideone.com/wD40Jy

aaa 12次 bb 21 cc 36 dd 24 ee 39
bbb 6次 a1 12 a2 16 a3 12

caozhy
caozhy   Ds   Rxr 2016.02.29 20:44

aaa bb 21 cc 36 dd 24
bbb a1 12 a2 16 a3 12
Press any key to continue . . .

audiohifi
audiohifi   2016.03.01 00:54

谢谢仁兄,我试试看

audiohifi
audiohifi   2016.03.02 22:49

aaa bb 10 cc 11 dd 12 ee 13
aaa cc 9 dd 4 bb 2 ee13
aaa cc 16 bb 9 dd 8 ee13
bbb a1 6 a2 9 a3 8
bbb a2 7 a3 4 a1 6
如果是这种结构呢 数据从数据库中取 该如何处理 实在太深奥了,望老师百忙中帮解答一下,多谢

audiohifi
audiohifi   2016.03.07 02:45

感谢caozhy老师,能否统计aaa bbb出现的次数写一下呢 随便能否把老师的联系方式私信给我呢,随便寄点小特产给老师,感谢您的回答

Why_boy01
Why_boy01   2016.03.11 20:10

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Q239660
{
class Program
{
static void Main(string[] args)
{
string data = @"aaa bb 10 cc 11 dd 12
aaa cc 9 dd 4 bb 2
aaa cc 16 bb 9 dd 8
bbb a1 6 a2 9 a3 8
bbb a2 7 a3 4 a1 6";
string result = string.Join("\r\n", data.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Split(' ')).SelectMany(x => Enumerable.Range(0, 3).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, b = x.GroupBy(y => y.b).Select(y => new { b = y.Key, c = y.Sum(z => z.c) }) })
.Select(x => x.a + " " + string.Join(" ", x.b.Select(y => y.b + " " + y.c.ToString()))));
Console.WriteLine(result);
}
}
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!