I am getting items from a sharepoint list, like below, and then appending these to a list of the object type. I can't help but feel this is a slow way to populate this list, looping through each list item, initialising it as its object type then adding it to the list to be returned.

        List<ObjectA> _listA = new List<ObjectA>();
        using (var ctx = spContext.CreateAppOnlyClientContextForSPHost())
            if (ctx != null)
                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml = @"<View><Query>Query Here</Query></View>";
                    ListItemCollection configItems = ctx.Web.Lists.GetByTitle("ListName").GetItems(camlQuery);

                    foreach (ListItem item in configItems)
                        _listA.Add(new ObjectA(item));
                catch (Exception ex)
                    string strEx = ex.Message;
            return _listA;

I have tried with the following kind of idea

List<ObjectA> _listA = configItems .Cast<ObjectA>().ToList();

But this produces the following error:

Specified method is not supported.

I feel like this could be a big overhead where I can improve the speed and performance. Any thoughts on how to improve this.


Here is my solution with Linq:

configItems.AsEnumerable().Select(x => ToObjectA(x)).ToList();

Though it is 2 years passed since the question was asked I faced the same issue.

Here's my code:

private static List<ListItem> getItems(List list, ClientContext ctx)
    ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
    ctx.Load(items, icol => icol.Include(i => i.DisplayName));

    var result = new List<ListItem>();
    for (int i = 0, len = items.Count; i < len; i++)
    return result;

As you can see I queried all items from the list and then return the result as a list.

At first, You should know that ToList() method from System.Core assembly implemented just like that: first some checks that collection is not null and has some elements, then calling List.Add() method for all elements in a sequence. So it wouldn't improve the perfomance if you could use ToList() method because it works almost exactly as your code.

Secondly, to proove for myself I made a collection of 1000 lists with items inside then checked my method. In case of converting items to List<ListItem> it took 203 ms in average for method to run. In case of returning raw ListItemCollection it took 202 ms.

  • Maybe I'm misreading this, but the original question here involves casting the list items to a new type, not converting from a ListItemCollection to a List<ListItem> – Robert Lindgren May 25 '17 at 14:09
  • @RobertLindgren, yeah that is how it usually works: items.Cast<ListItem>().ToList(); but won't help in case of ListItemCollection. – R. Matveev May 25 '17 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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