You should return the filtered result, so if your method looks like this:
public void Filter(List<Item> searchResults)
change it to:
public IEnumerable<Item> Filter(List<Item> searchResults)
and then return the result instead of assigning it to the variable. In other words, change this:
searchResults = (from item1 in searchResults
to this:
return (from item1 in searchResults
In your call, change from:
Filter(list)
to:
list = Filter(list).ToList();
This gives you maximum reusability, as you can pass in a list and trust the method not to change it, leaving the choice whether to place it into a new variable, or replacing the original list, up to you.
Also, as indicated by Fredrik in his comments, the call to .ToList()
in the Linq query should be removed as well, and possible you might want to change the input parameter to be IEnumerable as well.
Let me summarize.
If you have code like this:
public void Filter(List<Item> searchResults)
{
searchResults = (from item1 in searchResults
join item2 in coll
on item1.skuID equals item2.Skuid
where item2.SearchableValue == value
select item1).ToList();
}
...
list = ...
Filter(list);
and want to make list
become updated, I'd change the code to this:
__ changed ______ __ changed ______
public IEnumerable<Item> Filter(IEnumerable<Item> searchResults)
{
return from item1 in searchResults <-- changed
join item2 in coll
on item1.skuID equals item2.Skuid
where item2.SearchableValue == value
select item1; <-- changed
}
...
list = ... _ added__
list = Filter(list).ToList();