Home > Net >  C # how to an array of classified summary
C # how to an array of classified summary

Time:11-26

Such as an array of the row five columns have a 9 string [9, 5] Input_Data
Stored inside the
{1 class, zhang, male, 85, mathematics}
{1 class, li si, male, 72, Chinese}
{2 class, Cathy, female, 85, mathematics}
{2 class, zhang, female, 88, Chinese}
{1 class, zhang, male, 76, English}
{2 class, zhang, female, 98, mathematics}
{1 class, li si, male, 98, mathematics}
{2 class, Cathy, female, 95, English}
{1 class, zhang, male, 80, Chinese}
How to through the first three columns completely consistent, the fourth column sum score,
Produces the following results of string array [and] Output_Data
{1 class, zhang, male, 241, the total score}
{1 class, li si, male, 170, the total score}
{2 class, Cathy, female, 180, total score}
{2 class, zhang, female, 186, the total score}

CodePudding user response:

1, one end in the database directly through the query results;
2, traverse again, before the three as the key, the fourth as values, to generate a dictionary, does not exist, it added, there is added value,

CodePudding user response:

Consult the following, their top

CodePudding user response:

The most direct way is to set the two loops

CodePudding user response:

reference 1/f, 1210 near response:
1, one end in the database directly through the query results;
2, traverse again, before the three as the key, the fourth as values, to generate a dictionary, does not exist, it added, there is bonus,

Database query
Can say in detail

CodePudding user response:

In the database?
Select concat (a, b, c), sum (d) the from t group by concat (a, b, c)

CodePudding user response:

The original poster can consider to use Linq to get, actually I don't really understand this, when the future,
-
 
using System;
Using System. Collections. Generic;
Using System. The Data;
Using System. Linq;
Using System. The Text;
using System.Threading.Tasks;

The namespace ConsoleApp1
{
Class Program
{
Public static DataTable dt1=new DataTable (" tb1 ");
The static void Main (string [] args)
{
Dt1. Columns. The Add (new DataColumn (" bj ", typeof (string)));
Dt1. Columns. The Add (new DataColumn (xm, typeof (string)));
Dt1. Columns. The Add (new DataColumn (" xb ", typeof (string)));
Dt1. Columns. The Add (new DataColumn (" fs ", typeof (Int32)));
Dt1. Columns. The Add (new DataColumn (" km ", typeof (string)));

Add (dt1 "class 1", "zhang", "male", 85, "mathematics");
Add (dt1 "class 1", "li si", "male", 72, "Chinese");
Add (dt1 "class 2", "detective", "female", 85, "mathematics");
Add (dt1 "class 2", "zhang", "female", 88, "Chinese");
Add (dt1 "class 1", "zhang", "male", 76, "English");
Add (dt1 "class 2", "zhang", "female", 98, "mathematics");
Add (dt1 "class 1", "li si", "male", 98, "mathematics");
Add (dt1 "class 2", "detective", "female", 95, "English");
Add (dt1 "class 1", "zhang", "male", 80, "Chinese");

The DataSet ds1=new DataSet ();
Ds1. Tables. The Add (dt1);

Var query=the from d1 in ds1. Tables [0]. AsEnumerable ()
Group d1 by new
{
Bj=d1. Field (" bj "),
Xm=d1. Field Xm (" "),
Xb=d1. Field (" xb ")
}
Into s
Select new
{
Bj=s.S well (p=& gt; P.F ield (" bj ")). The First (),
Xm=s.S well (p=& gt; P.F ield Xm (" ")). The First (),
Xb=s.S well (p=& gt; P.F ield (" xb ")). The First (),
Fs=s.S um (p=& gt; P.F ield};

The foreach (var q in the query. ToList ())
{
Console. WriteLine (" class: {0} name: sex: {1} {2} score: {3} "total score, q.b j, q.x m, q.x b, q.f s.T oString ());
}

The Console. ReadKey ();
}
Public static void the Add (DataTable dt, bj, string string xm, string xb, int the fs, string km)
{
DataRow Dr=dt. NewRow ();
Dr [" bj "]=bj;
Dr [" xm "]=xm;
Dr [" xb "]=xb;
Dr [" fs "]=fs;
Dr [" km "]=km;
Dt. Rows. The Add (Dr);
}
}
}

  •  Tags:  
  • C#
  • Related