C # datatable Filters duplicate data for a column (equivalent to distinct)

Source: Internet
Author: User
1. Description of the problem

In the program code has a query out of the DataTable, call him a, the data structure is as follows:

A table
Date
Detection Method
Number of detections
01
A
10
01
B
11
01
C
8
02
A
13
02
B
9
03
A
7
03
B
12
03
C
11

What I'm going to do is process this a table to print the report and convert his data results to this form:

Table B
Date
a detection method
B Detection Method
C Method
01
10
11
8
02
13
9

03
7
12
11
2. Thinking:

It's a messy idea when you start doing it (in fact, it just comes up on a foreach loop and then you debug it). , wrote a few loops to find out seemingly can't, if can do, thank you very much you can leave a message we communicate together.

Then start looking at the relevant data and find that the C # operation DataTable can use a DataTable. Select ("column1=") to filter the data that you want to filter, conditions can have multiple, and the result returned is datarow[] collection.

Then look at the structure of table A, the date is duplicated, using datateble. Defaultview.todatatable (true,new string[] {"Column1"}) filters the data that needs to be filtered, which is equivalent to the distinct usage in SQL statements and can have more than one column.

Key syntax:

datarow[] Drarr = Datatable1.select ("clumn1= ' conditions '")//Conditionally query results
DataTable dt = DataTable1.DefaultView.ToTable (True, New string[] {"Column1"})//Filter data
3. Part of the code:
String a = "Select Car_zhdate,car_testtype,count (0) detect the number of detected methods per day from Tj_car_testtype GROUP by Car_zhdate,car_testtype or Der by Car_zhdate,car_testtype "//daily detection methods to detect how many cars string b =" Select A.car_zhdate, the first inspection of the number of days, the total number of initial inspection, ISNULL (each day Retest qualified quantity, 0 each day recheck qualified quantity, IsNull (each day recheck total quantity, 0) each day recheck total quantity from "+" (select Car_zhdate,count (0) each day first examines the qualified quantity fro
                            M tj_car_testtype where car_evl= ' T ' and Car_ccdate=car_zhdate and car_rettest= ' 1 ' GROUP by car_zhdate) a ' + "Left join" + "(select Car_zhdate,count (0) Total number of first checks from Tj_car_testtype where Car_ccdate=car_zhdate and car_rettest= ' 1 ' GROUP by car_zhdate) B "+" on a.car_zhdate = b.ca R_zhdate "+" LEFT Join "+" (select Car_zhdate,count (0) The number of days of retest qualified
                            From Tj_car_testtype where car_evl= ' T ' and Car_ccdate=car_zhdate and car_rettest= ' 2 ' GROUP by car_zhdate ' C ' + "On A.CAr_zhdate=c.car_zhdate "+" LEFT Join "+" (select Car_zhdate,coun
                            T (0) Total number of retest days from Tj_car_testtype where Car_ccdate=car_zhdate and car_rettest= ' 2 ' GROUP by car_zhdate) d "+

                "On A. Car_zhdate=d.car_zhdate"; DataTable AA = SqlHelper.ExecuteDataset (CommandType.Text, a).
                Tables[0]; DataTable BB = SqlHelper.ExecuteDataset (CommandType.Text, b).

                Tables[0];
                DataTable tempdb = new DataTable (); Tempdb.
                Columns.Add ("Ri", typeof (String)); Tempdb.
                Columns.Add ("ASM", typeof (String)); Tempdb.
                Columns.Add ("SDS", typeof (String)); Tempdb.
                Columns.Add ("BTG", typeof (String)); Tempdb.
                Columns.Add ("Sub", typeof (String)); Tempdb.
                Columns.Add ("Cjhgl", typeof (String)); Tempdb.

                Columns.Add ("Fjhgl", typeof (String)); String sub = ""; String cjhgl = ""; StringFjhgl = ""; foreach (DataRow Dr in Bb.) Rows) {sub = convert.tostring (Convert.ToInt32 (dr["Total number of initial checks]". ToString ()) + Convert.ToInt32 (dr["Total number of Days of review"].
                    ToString ())); Cjhgl = (Convert.todecimal (dr["The number of initial inspection of each day"). ToString ())/Convert.todecimal (dr["Total number of initial checks per day"]. ToString ())).
                    ToString ("P"); if (dr["total number of Days of review"].
                    ToString () = = "0") {Fjhgl = "no retest vehicle"; else {Fjhgl = (Convert.todecimal (dr["eligible quantity for each day"). ToString ())/Convert.todecimal (dr["Total number of Days of review"]. ToString ())).

                    ToString ("P"); } tempdb.
                Rows.Add ("", "", "", "", Sub, Cjhgl, FJHGL); } datarow[] Sdsarr; Datarow[] Asmarr;
                Datarow[] Btgarr; DataTable ritable = aa.
                Defaultview.totable (True, new string[] {"Car_zhdate"}); int ricount = ritable. Rows.coUnt for (int i = 0; i < Ricount i++) {Sdsarr = AA. Select ("car_zhdate= '" + ritable.) rows[i]["Car_zhdate"].
                    ToString () + "' and car_testtype= ' 0 '"); Asmarr = AA. Select ("car_zhdate= '" + ritable.) rows[i]["Car_zhdate"].
                    ToString () + "' and car_testtype= ' 1 '"); Btgarr = AA. Select ("car_zhdate= '" + ritable.) rows[i]["Car_zhdate"].
                    ToString () + "' and car_testtype= ' 4 '"); Tempdb. Rows[i]["ri"] = ritable. rows[i]["Car_zhdate"].
                    ToString (); if (Sdsarr. Length > 0) tempdb. rows[i]["SDS"] = sdsarr[0]["Number of detection methods per day"].
                    ToString (); else tempdb.
                    rows[i]["SDS"] = "0"; if (Asmarr. Length > 0) tempdb. rows[i]["asm"] = asmarr[0]["Number of detection methods per day"].
                    ToString (); else tempdb.
                    rows[i]["asm"] = "0"; If(Btgarr. Length > 0) tempdb. rows[i]["BTG"] = btgarr[0]["Number of detection methods per day"].
                    ToString (); else tempdb.
                rows[i]["BTG"] = "0"; }










Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.