Search code examples
c#sharepointsharepoint-2010

C# - How to combine SharePoint Lists in C#


I am trying to combine two SharePoint lists into one so I can return items from two separate lists as one entity.

I have multiple Objects but I haven't found a good way to combine them no matter which ones I use.

Is there a simple way to combine the following?

SPList pubList = web.Lists["public"];
SPList secureList = web.Lists["secure"];

or

SPListItemCollection pubFiles = retrieveDocs(web, meta, pubList);
SPListItemCollection secureFiles = retrieveDocs(web, meta, secureList);

or convert these DataTables to SPListItemCollections

DataTable pubTable = pubFiles.GetDataTable();
DataTable secureTable = secureFiles.GetDataTable();

pubTable.Merge(secureTable);

Solution

  • This post answered my question.

    In Summary

    using System.Linq;
    
    Then
    
    List<SPListItem> allItems = new List<SPListItem>();
    allItems.AddRange(list.GetItems(spQuery).Cast<SPListItem>().ToList());
    allItems.AddRange(list2.GetItems(spQuery).Cast<SPListItem>().ToList());
    
    //Do something to the "allItems"