2

I have two ListItemCollection objects created after calling SharePoint GetItems with a Caml query -

public Microsoft.SharePoint.Client.ListItemCollection GetItems(Microsoft.SharePoint.Client.CamlQuery query)

ListItemCollection list1 = GetQuery(...);
ListItemCollection list2 = GetQuery(...);

I want to combine list1 and list2 to have a final ListItemCollection object - to iterate over as -

   foreach (var listItem in itemCollection)
    {
        if (listItem.FieldValues.ContainsKey("FileRef"))          
        {
            items.Add(listItem["FileRef"] as string);
        }
    }

so I don't have to iterate twice.
How can I combine list1 and list2 to return a ListItemCollection object?
I tried using list1.Concat(list2), but it returns an IQueryable object.

Please guide.

  • What are you trying to accomplish with this? Are you only looking to display the results? – jpollar Jun 15 '15 at 15:36
  • question updated to add the usage details. – iniki Jun 16 '15 at 10:31
  • Would probably be easier to use CAML and SPSiteDataQuery instead. – Bunzab Jun 16 '15 at 11:28
  • You can't iterate over two different list within the same collection. If it's the same list, then you just need to rewrite your CAML query to include everything needed. – jpollar Jun 16 '15 at 14:09
1

There is a alternative, you can take datatable of each listitem collection using list1.getDataTable() and you can merge those data table and iterate it in a single for loop.

                    SPListItemCollection list1Coll = list1.Items;
                    SPListItemCollection list2Coll = list2.Items;


                    DataTable dTable1 = list1Coll.GetDataTable();
                    DataTable dTable2 = list2Coll.GetDataTable();

                    dTable1.Merge(dTable2);

                    foreach (DataRow dRow in dTable1.Rows)
                    {
                        Console.WriteLine(dRow["Title"].ToString());
                    }
| improve this answer | |
1

This is not exactly what you asked but it's the thing i usually do when faced with this kind of task: basically shove the items in a more dev friendly type to work with since the SPListItemCollection methods are rather scarce ...

var coll1 = SPList1.Items.Cast<SPListItem>();
var coll2 = SPList2.Items.Case<SPListItem>();
coll1.Concat(coll2);

the resulting coll1 will be not of type SPListItemCollection but IEnumerable<SPListItem> and this is actually not bad because we can perform all kinds of filtering, projection and other operations on it like for example:

coll1 = coll1.Where(listItem => listItem.FieldValues.ContainsKey("FileRef"));

which could replace: if (listItem.FieldValues.ContainsKey("FileRef")) because coll1 will only contain elements where this condition is true, so that you can just use it like:

foreach(var x in coll1) { items.Add(x["FileRef"] as string; }

PS: if you'd like another type for coll1 you could just use ToArray() to use square brackes for accessing the elements, or ToList() which is nice as well ;>

| improve this answer | |
0
  List<ListItemCollection> lst = new List<ListItemCollection>();
        ListItemCollection lst1 =GetQuery(...);
        ListItemCollection lst2 = GetQuery(...);
        ListItemCollection lst3 = new ListItemCollection();
        lst.Add(lst1);
        lst.Add(lst1);

        foreach (ListItemCollection item in lst)
        {
            foreach (ListItem newItem in item)
            {
                lst3.Add(newItem);
            }
        }

Thanks

| improve this answer | |
-1

Have you tried this:

list1.AddRange(list2);

This would combine two lists but does not remove any duplicates.

| improve this answer | |
  • 2
    There is no AddRange() method on a ListItemCollection object! – Robert Lindgren Jun 16 '15 at 12:32
  • Thanks Robert! Didn't notice the Microsoft.SharePoint.Client.ListItemCollection namespace. Yes there is no AddRange() method for Microsoft.SharePoint.Client.ListItemCollection object. For the above problem since we can't combine the lists, I would use the SPSiteDataQuery to join two lists in the CAML query. msdn.microsoft.com/en-us/library/… – Rakibism Jun 17 '15 at 5:29

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.