Question

My question is regarding the use of DataTable vs ListItemCollection. Basically I have a small solution, where I have to loop through the items in the list to verify if the items already exists or not.

At this time i'm doing like this, for every item i'm trying to add

libraryName.GetItems(query).Count >= 1

Someone advised me to use GetDataTable, basically DataTable to only retrieve once the data from the SharePoint and then to use it instead ... would it be a better approach? Or is there even better?

Secondly if i will use DataTable dt = listitemcol.GetDataTabel(); how can i then be able to retrieve data from the datatable what names will the columns have? internalnames or not?

Is it also possible to do a query on datatable with where statement?

Any input is appreciated.

Was it helpful?

Solution

The main difference between DataTable and SPListItemCollection is that your data in DataTable isn't connected to any SPWeb or SPSite objects. You can create SPWeb, get DataTable, dispose web and you will be sure that you will not have any memory leaks or errors.

You can select data from DataTable using DataTable.Select method.

Also if you want to ensure that field contains unique values you can just set EnforceUniqueValues property for this field to true.

OTHER TIPS

Shkipper,

Possible duplicate of the question is:
SPListItemCollection and GetDataTable method, this will answer your performance question!

If you use GetDataTable(), columns will have Internal Names and while debugging you can hover DataTable object and click DataView option to show complete data with columns!

You can first use SPQuery to get your filtered SPListItemCollection and then use GetDataTable to iterate through the items... And yes you can Filter and Sort Directly through DataTable and see DataView RowFilter Syntax [C#]

I hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top