Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.