Working with DataTables and Efficiency
I have a couple of questions regarding efficiency and performance. I appreciate your advise on this.
Question 1:
I am using LINQ. 'GetEntityList' is a stored procedure I am calling, and the result is an object that holds what the stored procedure returns.
'The code works perfect, but is that an efficient way to convert an enumerable object to a datatable? I just want to make sure I am on the right track.Dim Result = db.GetEntityList()
Dim resultList As List(Of GetEntityListResult) = Result.ToList
Dim dt As New DataTable
dt.Columns.Add("GenEntNumber")
dt.Columns.Add("GenEntName")
dt.Columns.Add("ComLegalName")
Dim dr As DataRow
For Each item As GetEntityListResult In resultListdr = dt.NewRow()
dr(0) = item.GenEntNumber
dr(1) = item.GenEntName
dr(2) = item.ComLegalName
dt.Rows.Add(dr)
Next
Question 2:
The following is a peice of code that filters 'MainDataTable' and creates a new datatable with the filtered Data. The code works perfect, but I am wondering if my code is efficient?Dim filteredTable As New DataTable
filteredTable.Columns.Add("TravCategory")
filteredTable.Columns.Add("TravCompany")
filteredTable.Columns.Add("TravAgency")
For Each DataRow In MainDataTable.Select("TravCategory='category1'")
dr = filteredTable.NewRow()
dr(0) = DataRow(0)
dr(1) = DataRow(1)
dr(2) = DataRow(2)
filteredTable.Rows.Add(dr)Next
Answers
- Question 1:
Yes, this is fine. The one potential improvement I see here would be to just avoid calling:
Dim resultList As List(Of GetEntityListResult) = Result.ToList
There is no reason to create the list in this case. Just do your foreach on Result directly, so you never create the List.
Question 2:
This looks very reasonable, and reasonably efficient.
Reed Copsey, Jr. - http://reedcopsey.com- Marked As Answer byN.Afache Tuesday, November 03, 2009 7:53 AM
All Replies
- Question 1:
Yes, this is fine. The one potential improvement I see here would be to just avoid calling:
Dim resultList As List(Of GetEntityListResult) = Result.ToList
There is no reason to create the list in this case. Just do your foreach on Result directly, so you never create the List.
Question 2:
This looks very reasonable, and reasonably efficient.
Reed Copsey, Jr. - http://reedcopsey.com- Marked As Answer byN.Afache Tuesday, November 03, 2009 7:53 AM


