Reputation: 6216
I have some code where I apply a "where clause" like this:
I put in "n" as an example of the database table.
List<KeyValuePair<int, int>> n = new List<KeyValuePair<int, int>>();
n.Add(new KeyValuePair<int, int>(1,2));
n.Add(new KeyValuePair<int, int>(1,3));
n.Add(new KeyValuePair<int, int>(4,6));
n.Add(new KeyValuePair<int, int>(4,3));
n.Add(new KeyValuePair<int, int>(5,3));
var zzz = n.Where(z => z.Key == 1); // this returns "1,2" and "1,3"
Then somewhere else in my code I did this:
zzz.Where(x => x.Value == 3); // this should return "1,3"... Instead it seems to return "4,3" and "5,3" and "1,3".
Isn't the second Where supposed to return only "1,3"??? The second Where clause, should be applied to the result of the "zzz" shouldn't it?
Upvotes: 1
Views: 623
Reputation: 564631
The second Where clause, should be applied to the result of the "zzz" shouldn't it?
Yes, and it in fact does. Take this sample code:
List<KeyValuePair<int, int>> n = new List<KeyValuePair<int, int>>();
n.Add(new KeyValuePair<int, int>(1,2));
n.Add(new KeyValuePair<int, int>(1,3));
n.Add(new KeyValuePair<int, int>(4,6));
n.Add(new KeyValuePair<int, int>(4,3));
n.Add(new KeyValuePair<int, int>(5,3));
var zzz = n.Where(z => z.Key == 1); // this returns "1,2" and "1,3"
zzz = zzz.Where(x => x.Value == 3); // this then filters to the 2nd option
foreach(var pair in zzz)
Console.WriteLine("{0}:{1}", pair.Key, pair.Value);
This prints 1:3
, as expected.
I suspect the problem may be that you're not re-assigning the results of the second filter: zzz.Where(x => x.Value == 3);
The result of that needs to be assigned to a variable (or enumerated) in order to see the actual, filtered results.
Upvotes: 2