ā want to add a conditional .Take(10) to a query without duplicating the code
Im looking for an easy way to add a .Take(10) or .Take(x) to a iqueryable so that my test go faster instead of running on the entire data.
is there a way to make it so if x is null it takes all or ignres the take() option ??
14 Replies
"so that my test go faster instead of running on the entire data". What do you mean?
well lets say my code is
context.Customers.Tolist().Foreach( DO A LOT OF STUFF )
I would like to add a .Take(10) but only if i passed an argument that says to run this in test
i could do
if (isTest){
context.Customers.Take(10).Tolist().Foreach( DO A LOT OF STUFF )
}
else{
context.Customers.Tolist().Foreach( DO A LOT OF STUFF )
}
but i have to change tons of queries of this kind so I was wondering if theres a way to put the Take() and basically takeAll unless otherwise specified
ok so its not builtin
no, ofc not
its a bit weird
š
Do ToList() after the ifs, just to see what happens
Also, weird requirement that "test" thing
the thing is i dont want a if lol
so use the extension method provided above
but its a bit weird. in general your approach should be that tests provide their own data
yea that's basically what I wanted but I thought there might have been something of this kind already existing in LINQ
It's just for you to understand what happens. Since in your example you did multiple ToList(), I don't think you understand what fundamentally happens
its ok dont worry about it
its not a format unit test, im just gonna change it to 'test mode' and run it to see something
i see
formal*
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.