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)
{
try
{
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View><Query>Query Here</Query></View>";
ListItemCollection configItems = ctx.Web.Lists.GetByTitle("ListName").GetItems(camlQuery);
ctx.Load(configItems);
ctx.ExecuteQuery();
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.