My search result becomes case sensitive when I change var to IEnumerable; consider below code:
the code below returns both cases with RA or ra:
var result = from x in dataBase.tableName
select x;
result = result.Where(x => x.id.Contains("ra"));
the code below returns only cases with ra and not RA:
IEnumerable result = from x in dataBase.tableName
select x;
result = result.Where(x => x.id.Contains("ra"));
Could someone help and explain me what is going on? shouldn’t the result be similar?
The difference is that in the first case,
varwill be anIQueryable<T>– which means yourWherecall will be toQueryable.Where(after converting your lambda expression to an expression tree). The filter ends up being executed in the database, where I suspect your field is case-insensitive (and so searching is case-insensitive).In the second case, you’ve got an
IEnumerable<T>, so yourWherecall will be toEnumerable.Where(after converting your lambda expression to a delegate). The filter ends up being executed in .NET code instead – whereContainsis always case-sensitive.Basically, LINQ is a leaky abstraction – yes, it would be lovely if the same filter gave the same results in all situations, but that’s unrealistic. Learn where the leaks are, and act accordingly – LINQ is still a huge boon 🙂