Sqlite PCL and Linq - Is SQLite.Net Table method loading the entire table into a collection?
Apparently, it's not that obvious. I've just found a very interesting forum post here
To summarize, if you do this
var whereFirstOrDefault = Table<AlertType>().Where(a => a.Name.Equals(alertType.Name)).FirstOrDefault();
It is very different than doing this
var firstOrDefault = table.FirstOrDefault(a=> a.Name.Equals(alertType.Name));
The first query generates this command and does not retrieve the whole table:
select * from "AlertType" where ("Name" = (?)) limit 1
However, the second query is :
select * from "AlertType"
As mentionned,
"SQLite.TableQuery has an extension method for 'Where' which takes a predicate."
That means that Linq will modify the sql statement in consequence.
"But SQLite.TableQuery only has a FirstOrDefault that doesn't take parameters:"
It means that, if you use FirstOrDefault
with a predicate, it will retrieve the whole table but if you use it without a predicate on a tablequery, it will modify the sql statement
"If you call FirstOrDefault with a predicate (my second approach), SQLite.NET selects the entire table and then uses LINQ To Objects to do the FirstOrDefault on the collection in memory."