Why the local variable?

advertisements

resharper offers me to make this a local variable, and writes "access to modified closure"

if (filter != null)
{
    if (filter.CityId != 0)
    {
        ads = ads.Where(x => x.Ad.CityId == filter.CityId);
    }
    if (filter.BusinesCategoryId != 0)
    {
        ads = ads.Where(x => x.BusinessCategoryId == filter.BusinesCategoryId);
    }
}

Why do local variable filter?


Because your query(Where(...)) is not being executed. I assume filter is obtained from a loop?

Linq query are not executed until they are used. so if you looped though a bunch of filters then started executing them later, the filter value would be wrong in the query.

A similar question : Access to Modified Closure Also: http://devnet.jetbrains.net/thread/273042

would need to see more code to 100% sure.