2

I have the following class to store objects from a REST API call from SharePoint:

 [Serializable]
public class DocumentSearchResult
{
    public string TotalCount { get; set; }
    public string DocumentPath { get; set; }
    public string DocumentTitle { get; set; }
    public string DocumentSize { get; set; }
    public string DocumentAuthor { get; set; }
    public string DocumentDescription { get; set; }
    public string DocumentFileExtension { get; set; }
    public double DocumentRank { get; set; }
    public Int64 DocumentDocId { get; set; }
    public Int64 DocumentWorkId { get; set; }
    public DateTime DocumentWrite { get; set; }
    public string DocumentParentLink { get; set; }
    public DateTime DocumentLastModifiedDate { get; set; }
    public string DocumentFileType { get; set; }

    //These next set of properties are used for Viewing the results in embedded or preview
    public string DocumentRedirectedEmbededURL { get; set; }
    public string DocumentRedirectPreviewURL { get; set; }
    public string DocumentRedirectURL { get; set; }

}

I create a list of these objects in my code:

var docReturnResult = new List<DocumentSearchResult>();

I have another list I create using:

var filterList = this.Where(x => x.TenantId == TenantId);

this will return an IQueryable list that contains a value I need to filter the second list. The filterList has an attribute called SharePointId (x => x.SharePointId) that I need to use to filter the docReturnResult list. So I need to compare the filterList SharePointId to the docReturnResult DocumentDocId and remove any objects in the docReturnResult list that don't match the SharePointId's in the filterList.

This is what I tried last:

 var trimResults = new DocumentSearchResult();
            trimResults = docReturnResult.RemoveAll(x => x.DocumentDocId != filterList.Where(y => y.SharePointId));

but I get an error:

Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type.

Any help is much appreciated.

1 Answer 1

2

Try this, it should remove all documents from docReturnResult where match is not found in filterList

docReturnResult.RemoveAll(x => !filterList.Any(y => y.SharePointId == x.DocumentDocId));

Root cause of your error -

 x.DocumentDocId != filterList.Where(y => y.SharePointId)

Right hand side will return IQuarable object and you are trying to compare it with DocumentDocId which will not work.

Edit You don't need a new variable trmResults, as RemoveALL on docReturnResult will trim the same object, so you can just return docReturnResult

return docReturnResult.RemoveAll(x => !filterList.Any(y => y.SharePointId == x.DocumentDocId));
Sign up to request clarification or add additional context in comments.

3 Comments

good answer, although assigning the result of RemoveAll to trimResults would not work because RemoveAll returns an int representing the number of elements removed from the List<DocumentSearchResult> whereas trimResults is of type DocumentSearchResult. just docReturnResult.RemoveAll(x => !filterList.Any(y => y.SharePointId == x.DocumentDocId)); should suffice.
@Aominè - I just overlooked the trimResult declaration, thanks for pointing out. updated answer.
Thanks for the answer and comments as well as the explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.