score:9
The difference is that in the first case, var
will be an IQueryable<T>
- which means your Where
call will be to Queryable.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 your Where
call will be to Enumerable.Where
(after converting your lambda expression to a delegate). The filter ends up being executed in .NET code instead - where Contains
is 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 :)
score:7
Your result
variable is not an IEnumerable
- it is an IQueryable
.
The result is changing when you manually change the type to IEnumerable
since you now do the following Contains
query with Linq to Objects and not on the database with Linq to Entities (or Linq to SQL).
With IEnumerable
your query is using your lambda expression, bringing each result from the DB into memory and executing the case sensitive Contains()
extension method.
With IQueryable
your query is using an expression tree and will try to create a corresponding database query with your Contains
query which happens to be case insensitive (most likely a SQL LIKE
query).
Just making the implicit type explicit as IQueryable
will fix your problem.
Source: stackoverflow.com
Related Articles
- different search result by changing var to IEnumerable
- Can I use a LINQ IEnumerable result as the data source for a Gtk.TreeView?
- Entity Framework, Code First and Full Text Search
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- Serializing result of a LINQ IEnumerable
- In which cases do I need to create two different extension methods for IEnumerable and IQueryable?
- IQueryable<T> gives different result than a List<T>
- using Comparer to sort IEnumerable in C# by different fields
- Is it possible to perform a LINQ aggregate over a collection of one type to a different result type?
- How to intersect two different IEnumerable collections
- Linq search result by closest match
- LINQ Source Code Available
- Why do these three pieces of LINQ code produce different (or erroneous) results?
- .NET 4 Code Contracts: "requires unproven: source != null"
- LINQ query return same result despite different datasources
- Changing a piece of code into expressions
- Can Linq `Where` clause query result be different depending on whether query is executed lazily or non-lazily?
- Retrieve bool result by using LinQ code
- Search between Two IEnumerable - LINQ
- entity framework returning different result when predicate is passed
- Linq (or SQL): Get a search query and sort it order by best result
- creating Linq to sqlite dbml from DbLinq source code
- c# Different Result between Linq and SQL
- Running different code depending on what Net Framework version is installed
- How can i copy data table records of different field name based on mapping list evaluating condition on source data table?
- C# Linq GroupBy ==> Class vs Anonymous class --- different result
- Using LINQ query result for data source for GridControl c#
- SQL subquery result in LINQ and Entity Framework Code First
- How to break complex query into different methods to achieve less code complexity
- linq to entities changing database connection string in code
- How to create a DropDownList from a LINQ query
- How to get dictinct userid with order by close using Linq
- Using LINQ to query XML file
- Check if column exists before execute select clause
- How to calculate number of leap years between two years in C#
- Is there a way to use Any() in this LINQ query?
- LINQ track changes
- LINQ SQL Connection String Not Working When Read From TXT File
- Linq join enumerable with entity
- The entity or complex type 'xyz' cannot be constructed in a LINQ to Entities query
- Linq query populate collection from three tables
- Nested list database query in linq
- How to select both boolean values or one
- If it exists, .Remove() it, if not .Add() it
- Create list with data from grouped Linq query plus related data from another table
- Query and map complex objects
- LINQ/Lambda query help for SQL Group By + MAX?
- How to evaluate the pros and cons when changing language
- How does one tell if a LINQ extension method is suffering from double enumeration?
- How to include counted property in a query and rename the column in the result in c#