How to convert IEnumerable variable into Datatable?

I have data in excel containing nic number and gender. I want to group the data according to gender. Which I have achieved using linq query:
(
From row In dt
Group row By k=row(“Gender”).ToString.Trim
Into grp=Group
Select grp
)

But the result is saved as IEnumerable variable and I want to write result in Excel File.

@faraz.ahmed9

Main
├─ Read Range (ReadExcel)
│ └─ Input: “path_to_your_excel_file.xlsx”
│ └─ Output: dtData (DataTable)

├─ Assign genderGroups
│ └─ (From row In dtData.AsEnumerable()
│ Group By gender = row.Field(Of String)(“gender”) Into Group
│ Select Group.ToList()).ToList()

└─ For Each (group In genderGroups)
└─ Log Message
└─ "Group: " + group.Key
└─ "Count: " + group.Count.ToString()
└─ “Details: "
└─ String.Join(”, ", group.Select(Function(r) "NIC: " + r.Field(Of String)(“nic number”) + ", Gender: " + r.Field(Of String)(“gender”)))

male female.zip (168.6 KB)
Hi Faraz,
I have made Some changes to your query this should work, Please try this.
(
From row In dt
Group row By k=row(“Gender”).ToString.Trim
Into grp=Group
Select grp.ToList
).SelectMany(Function(x) x).CopyToDataTable

Thanks

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.