R – Strange LINQ behavior – ToList returning empty set


I cant understand why resultSet2 is empty! Only the first assert passes!

List<Tree> resultSet1 = this.datacontext.Trees.Where(t=>t.RiskRating.Contains("bad")).ToList();

Assert.IsTrue(resultSet1.count() == 3);

List<Tree> resultSet2 = this.datacontext.Trees.ToList().Where(t=>t.RiskRating.Contains("bad")).ToList();

Assert.IsTrue(resultSet2.count() == 3);



Best Solution

What does this.datacontext.Trees.ToList().Count() return?

Could it be something to do with your collation back at the database? The first example will convert the Contains("bad") method back to SQL, which might be case insensitive and return rows that contain "BAD" or "Bad". The second example wouldn't be case sensitive.

It'd be interesting to see what the values for RiskRating are back in the database, and what the SQL query getting executed looks like.

Related Question