1

I have a data table which I was to split as multiple datatables. The structure inside the datatable has three columns

Company Name. Emp Name and Phone

I want to split the whole datatable into multiple datatables each containing all the employees from one company. I hope I am making sense here with my question. I understood linq is the best way to go about doing this, but I have never used linq before and have completely no idea of how to go about doing this. Can some one give me a lead on how to go about this? Some code example would be highly appreciated.
Thank you

3
  • Is the list of available companies static? Why do you want to split it up into N datatables which you'll have to keep track of in another array or something anyway? (And by DataTable, you mean the .NET object System.Data.DataTable, right?) Commented Jan 24, 2013 at 15:28
  • Are you talking about some form of database sharding? If so this should be kept transparent from your linq queries. Or are you trying to keep each company separate manually? Commented Jan 24, 2013 at 15:28
  • I think I didnt get through very clear. My exact requirement is that I want to generate an excel file for each company to send them. I assumed that splitting my datatable (System.Data.DataTable) into different datatables would help as I know how to generate excel files from datatables. Is there any other way to do it? Thank you Commented Jan 24, 2013 at 15:35

1 Answer 1

3

You can get your company names and data tables as dictionary using this:

Dim dict = dataTable.AsEnumerable().GroupBy(Function(r) r.Field(Of String)("CompanyName")).ToDictionary(Function(g) g.Key, Function(g) CopyToDataTable)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the help. I did figure it out. I would just like to point out the reference to System.Data.DataSetExtensions to get it for any other users who are looking for it. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.