I am working on filtering a list using LINQ and came across a strange problem.
The code looks as follows:
filteredUsers = users.Where(x => x.ResourceTypeUserVms
.Any(x => x.ResourceUserVms
.Any(x => x.MetaData.ToLower().Contains(value))))
.ToList();
filteredUsers = filteredUsers
.Select(x =>
{
x.ResourceTypeUserVms = x.ResourceTypeUserVms.Select(y =>
{
y.ResourceUserVms = y.ResourceUserVms.Where(s => s.MetaData.ToLower().Contains(value)).ToList();
return y;
}).ToList();
x.ResourceTypeUserVms = x.ResourceTypeUserVms.Where(z => z.ResourceUserVms.Count() > 0).ToList();
return x;
}).ToList();
The users list contains the original data and the filteredUsers should only contain the filtered data. However, when I clear the filter (that means the original data should be displayed again), the users list seems to be manipulated.
Can anyone explain how I get the users list to stay the same no matter what?