Tuesday, August 10, 2010

LINQ and TSQL behavior difference

I found a lot, but for now record only this one.
When I say "LINQ" I mean LINQ-To-Object, not LINQ-To_Sql.
It took me quite a while to figure out why SQL request produces more records than LINQ request to preliminary loaded same data.
SQL:
SELECT aaa, bbb FROM TABLE WHERE bbb='xxx'; // returns 14 records
LINQ: I preloaded the whole TABLE into list, then query it:
var records = list.AsQueryable()
.Where("bbb==\"xxx\"", null)
.Select("new (aaa, bbb)", null); // returns 12 records

After all I found out that database contains 2 records with bbb='xxx     ' (spaces at the end)
TSQL ignores spaces, LINQ - does not, thus producing 2 less records.

No comments:

Post a Comment