The problems and requirements are as follows:
The data tables are as follows:
| ID |
Owner |
Resource |
Orderid |
Quotation |
| 1 |
Jack |
China |
363 |
45 |
| 2 |
Jack |
China |
363 |
45 |
| 3 |
Mike |
Japan |
678 |
23 |
| 4 |
Mike |
Japan |
678 |
23 |
| 5 |
Mike |
Japan |
678 |
23 |
| 6 |
John |
China |
893 |
12 |
| 7 |
John |
Korea |
453 |
11 |
| 8 |
Mike |
China |
234 |
78 |
| 9 |
Jack |
China |
676 |
33 |
| 10 |
Mike |
Japan |
454 |
25 |
First, re-query the owner, resource, orderid, and quotation to remove duplicate orders (but retain any of the IDCs)
| ID |
Owner |
Resource |
Orderid |
Quotation |
| 1 |
Jack |
China |
363 |
45 |
| 3 |
Mike |
Japan |
678 |
23 |
| 6 |
John |
China |
893 |
12 |
| 7 |
John |
Korea |
453 |
11 |
| 8 |
Mike |
China |
234 |
78 |
| 9 |
Jack |
China |
676 |
33 |
| 10 |
Mike |
Japan |
454 |
25 |
Then, the owner and resource of the two groups are used to group and count the total quantity of quotation. The results are displayed in the vertical and horizontal tables. The first row on the left is the name of the person, and the source is displayed in the top row.
| |
China |
Korea |
Japan |
| Jack |
78 |
0 |
0 |
| John |
12 |
11 |
0 |
| Mike |
78 |
0 |
48 |
I have not solved this problem for several days. The solution below is a work und. You are welcome to criticize and correct it. Please also give your suggestions.
Check. LINQ uses the distinct method, but this distinct method does not actually support any parameters.
VaR result = (from R in sampletable
Select New {R. Owner, R. Resource, R. orderid, R. Quotation}
). Distinct (). tolist ();
In this way, the record set of repeated orders is obtained, but it does not meet the requirements of the questions, because the questions require to retain any ID of the Repeated Records.
Therefore, use the join function of LINQ to match IDS by conditions. In this way, I used two steps. I don't know if I can do this in one step.
Let me take a look. So far, we have prepared for the second requirement and re-checked it.
I started to write a LINQ statement using nested group,
VaR result2 = from S in reslut
Group S by S. Owner into groupA
Select New {
Name = groupA. Key,
Newdata = from T in GroupA
Group T by T. Resource into groupB
Select New {
Resource = groupB. Key
Amount = groupB. quatation. sum ()}
}
A dataset with a nested structure is generated through nesting of two group by statements, which can be displayed on the page in the form of a master table.
The specific method can be nested with two listview, which is specified as result2 In the datasource of the outer listview,
When adding a Data Binding event that changes the data source of the inner listview, Microsoft's help is also written in this way, which can be implemented in principle, but it is already obvious to me!
Google again and found that I didn't need to use this nested group by. In fact, I first used a group by statement to query a dataset and use listview to display it, then define a group by in the outer listview, which is equivalent to writing the embedded group by to the front-end interface control, so that the embedded listview will naturally switch the dataset. This method seems simple. The disadvantage is that the nested logic is completed using the front-end space. For details, refer to the blog of Matt berseth.Article,
Http://mattberseth.com/blog/2008/01/building_a_grouping_grid_with.html
The problem arises again. My requirement is a vertical table. However, if one set is equal to a vertical table, the nested tables are arranged vertically. I have found a lot of information that I cannot solve.
I remember that I only needed to use multiple fields 'group by' in an SQL statement, but now I need to use the nested 'group by' clause for the SQL statement!
In addition, the structure of the queried nested structure is extremely inconvenient to display, which is too difficult.
I can write SQL statements as follows:
Select owner, resource, sum (quotation) from table
Groupby owner, Resource
I can directly display the query results because they are not nested structures.
Even the query results of nested structures can be directly output using the traditional ASP method or the new MVC method. The current form-based ASP. net, but I have to find a page control, which is too complicated.
No. Go to the page and check if the function supports multiple columns of groupby. Finally found this Australian blog,
Http://ddkonline.blogspot.com/2008/04/linq-group-by-syntax-for-grouping-on.html
You can write it like this,
VaR result3 = from C in table
Group C by new {C. Owner, C. Resource} into groupc
Select New {
Name = groupc. Key. Owner,
Country = groupc. Key. Resource
Amount = groupc. Count ()
}
The result is not a horizontal table, but the method is relatively simple.
Some experts have read and expressed your opinion to see if I have not taken the opposite direction?