Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

Can you help on how to to do this using linq in vb.net?
The Datatable1 columns should be group same like Datatable2 result.

Datatable1

ID# | NAME | DEPT
E12345 | Joe | HR
E12345 | Joe | HR
E12345 | Joe | HR
T54333 | Fred | IT
T54333 | Fred | IT
I22544 | Kate | AC
I22544 | Kate | AC

Expected result
Datatable2
ID# | NAME | DEPT
E12345 | Joe | HR
T54333 | Fred | IT
I22544 | Kate | AC

What I have tried:

Tried below code but the column NAME, DEPT has system error result.

Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn() _
            {
                New DataColumn("ID#", Type.GetType("System.String")),
                New DataColumn("Name", Type.GetType("System.String")),
                New DataColumn("Dept", Type.GetType("System.String"))
            })

        dt.Rows.Add(New Object() {"E12345", "Joe", "HR"})
        dt.Rows.Add(New Object() {"E12345", "Joe", "HR"})
        dt.Rows.Add(New Object() {"E12345", "Joe", "HR"})
        dt.Rows.Add(New Object() {"T54333", "Fred", "IT"})
        dt.Rows.Add(New Object() {"T54333", "Fred", "IT"})
        dt.Rows.Add(New Object() {"I22544", "Kate", "AC"})
        dt.Rows.Add(New Object() {"I22544", "Kate", "AC"})

        Dim groupID = dt.AsEnumerable() _
        .GroupBy(Function(x) x.Field(Of String)("ID#")) _
        .Select(Function(grp) New With
               {
                    .ID = grp.Key,
                    .Name = grp.Select(Function(x) x.Field(Of String)("Name")),
                    .Dept = grp.Select(Function(x) x.Field(Of String)("Dept"))
               }) _
            .ToList()

        Dim dtF As DataTable = dt.Clone()

        For Each item In groupID
            Dim dr = dtF.NewRow()
            'dr("ID#") = item.ID
            'dr("Name") = item.Name
            dtF.Rows.Add(item.ID, item.Name, item.Dept)
        Next

        DataGridView1.DataSource = dtF
Posted
Updated 24-Oct-19 21:03pm

var distinctObj = customer.Select(m => new {m.Id, m.Name,m.Dept}).Distinct().ToList();
 
Share this answer
 
Comments
Maciej Los 25-Oct-19 3:34am    
Well...
This question has been tagged as: VB.net, BUT(!) you showed an example in c#.
You need to add FirstOrDefault() method to get single Name and Dept instead of list of them.

VB
Dim groupID = dt.AsEnumerable() _
.GroupBy(Function(x) x.Field(Of String)("ID#")) _
.Select(Function(grp) New With
       {
            .ID = grp.Key,
            .Name = grp.Select(Function(x) x.Field(Of String)("Name")).FirstOrDefault(),
            .Dept = grp.Select(Function(x) x.Field(Of String)("Dept")).FirstOrDefault()
       }) _
    .ToList()
 
Share this answer
 
Comments
icavs 25-Oct-19 3:24am    
You're so great @Maciej Los.
It works! Thanks a lot.
Maciej Los 25-Oct-19 3:32am    
You're very welcome.

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