Error while casting the object RRS feed

  • Question

  • hello guys,

    I am getting below error while casting the object:

    Unable to cast object of type 'WhereListIterator`1[Microsoft.SharePoint.Client.ListItem]' to type 'Microsoft.SharePoint.Client.Folder'.

    code looks like :

    List<ListItem> items = new List<ListItem>();
    var filteritems = (Microsoft.SharePoint.Client.Folder)items.Where(tt => tt.FieldValues["FileRef"].ToString().Equals(Request.QueryString["FolderName"], StringComparison.CurrentCultureIgnoreCase));

    Wednesday, June 12, 2019 11:16 AM

All replies

  • The "Where" returns a whole set of elements. Even if you know that the condition will only be met by a single element, the compiler does not know it. It will return an iterator even if the iterator loops over a single element.

    You can solve it by adding .FirstOrDefault() at the end of the statement (or .First() or .Single() or .SingleOrDefault() depending on what you expect from your query).

    However, if "items" is a list of ListItem, this will still fail unless there is an automatic conversion from your class ListItem to type Microsoft.SharePoint.Client.Folder. If such a conversion does not exist, the cast operator will not be able to "invent" such a conversion for you.

    Wednesday, June 12, 2019 12:11 PM
  • Hi Kodnil,

    Thank you for posting here.

    For your question, you want to cast the object by using the above code.

    Based on my search, we could not convert Microsoft.SharePoint.Client.ListItem into Microsoft.SharePoint.Client.Folder. Because the code don't meet the requirements in the following link.

    Hope my explanation could be helpful.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, June 13, 2019 3:18 AM