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?
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)
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
精彩评论