Click here to Skip to main content
15,923,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datatable like below:

Name        Address
Name1       Address1
Name2       Address2
Name3       Address1
Name4       Address3
Name5       Address2
Name6       Address4


I want the above table like:

Name        Address
Name1       Address1
Name3       
Name2       Address2
Name5       
Name4       Address3
Name6       Address4


What I have tried:

I have tried like this:

dt = dt.AsEnumerable().OrderBy(r => r.Field<string>("Craft")).CopyToDataTable();


but it's only getting this below table:

Name        Address
Name1       Address1
Name3       Address1
Name2       Address2
Name5       Address2
Name4       Address3
Name6       Address4


and then sort Names (according to Address)

and the Address value should display in middle(only once) adjacent to names (under address column only)

remove the duplicate values in Address column Like:


Name      Address
Name1       Address1
Name3       
Name2       Address2
Name5        



this is what i want
Posted
Updated 1-Aug-17 2:51am
v2

1 solution

That's not a Group: grouping condenses information into summaries, not collects them together. This may help you understand the idea - it's for SQL queries, but its; exactly the same principle: SQL GROUP By and the "Column 'name' is invalid in the select list because..." error[^]

What you are trying to do is sort the records, not group them, for which you want OrderBy:
dt = dt.AsEnumerable().OrderBy(r => r.Field<string>("Address")).CopyToDataTable();
 
Share this answer
 
Comments
Kingshuk_SP 1-Aug-17 7:47am    
I need it in a bit modified way like below:

Name Address
Name1 Address1
Name3
Name4 Address2
Name2 Address3
Name5 Address4

like above, Address1 should appear once.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900