This should be quite easy, however I am failing to see why all my methods are not working.
I have looked at all the solutions and used them appropriately however am not getting the result.
solutions include
Solution 1
Solution 2
Here is the code:
IEnumerable<feature> available = _repo.GetAvailableFeatures();
IEnumerable<feature> selected = _repo.GetSelectedFeatures();
var filteredList = (available.Except(selected)).ToList;
var availableList = available.ToList();
var selectedList = selected.ToList();
availableList.RemoveAll(item => selectedList.Contains(item));
for (var i = 0; i < availableList.Count - 1; i++)
{
foreach (var t in selectedList)
{
if (availableList[i].Id == t.Id)
{
availableList.RemoveAt(i);
}
}
}
public class Feature
{
public int Id;
public int Desc;
}
When you use Except
you need to define what "equal" means for the feature
type. In your loop you define "equal" as "Id
s are equal", so you could:
Equals
and GetHashCode
in the feature
class
IEqualityComparer<feature>
Use Where
instead of Except
:
var filteredList = available.Where(a => !selected.Any(s => s.Id == a.Id))
.ToList();