(Example) There is a table A, and I want to use Distinct with the company code and department code
Column 1, Column 2
(1)001,A
(2)002, A
(3)001,C
(4)003,B
(5)004,D
(6)001, A
(7)002, A
//Create DataTable ATImage of the process of retrieving data from ATable
DataTablet = A table;
// Create DataView
DataView dv=dt.DefaultView;
//Distinct
DataTable resultDt=dv.ToTable(true, "Column1", "Column2");
(1)001,A
(2)002, A
(3)001,C
(4)003,B
(5)004,D
I could understand the above results by checking the code, but after changing the code as follows,
//Distinct
DataTable resultDt=dv.ToTable(true, "Column2");
The output looks like this and Column 1 is completely gone.
(1)A
(3)C
(4)B
(5)D
How do I implement it in order to make a duplicate decision in Column2 in Table A and output it as follows?
(1)001,A
(3)001,C
(4)003,B
(5)004,D
Note:
c#
Group by
sounds like a story.
I found a post doing something similar at home.stackoverflow.com/a/19407930/10882610
Example coding to Group by
on Linq:
dt=dt.AsEnumerable()
.GroupBy(r=>new{Col1=r["Col1"],Col2=r["Col2"]})
.Select(g=>g.OrderBy(r=>r["PK"]).First())
.CopyToDataTable();
This post was edited based on @wata's Comment and posted as Community Wiki.This post was edited based on @wata's Comment and posted as Community Wiki.
© 2024 OneMinuteCode. All rights reserved.