Reputation: 21999
I have a list of dynamic data. I would like to loop through the list to create a where clause. How do I do that?
IQueryable<MyObject> result = originalData;
foreach (var item in Years)
{
result = input.Where(x => x.Year == item).AsQueryable();
}
2013 should have 30 and 2014 should have 18. My result only gives 18 when it should be 48. Any ideas?
EDIT
I should mention I know I am overwriting the result. I tried += and I get the error that '+=' cannot be applied to operands of IQueryable(MyObject> and IQueryable(MyObject>
Upvotes: 0
Views: 74
Reputation: 223282
You don't need a for
loop, you can do:
IQueryable<MyObject> result = input.Where(x=> Years.Contains(x.Year));
Currently your result
is holding the last value of your loop. So suppose your last value in Years
is 2014, then you get back 18 rows against it, the previous rows are overwritten in the iteration.
Upvotes: 2