Himberjack
Himberjack

Reputation: 5792

How to remove from LINQ results, matching elements from array

I have LINQ result, and I have List. I want to do Where() on the LINQ and to REMOVE all the matching strings from the List.

I get errors of: Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator

What can I do?

Upvotes: 1

Views: 2988

Answers (2)

Rob Fonseca-Ensor
Rob Fonseca-Ensor

Reputation: 15621

You need to make a copy (with ToList, which is faster than ToArray) of the items you're removing:

var removeMe = list.Where(...).ToList();

Now removeMe wil be safe to iterate through, however, a better approach would be to use the List.RemoveAll method, because then you don't need any temporary data structures

Upvotes: 2

SLaks
SLaks

Reputation: 887451

You need to call AsEnumerable() or ToList() before your Where call to force LINQ-to-SQL to download all results to the client and perform a local Where.

(Since your Where call cannot be performed on the server, you need to do it on the client)

Upvotes: 3

Related Questions