[英]Sqlite PCL and Linq - Is SQLite.Net Table method loading the entire table into a collection?
I'm a building a windows phone 8 application. 我是一个构建Windows Phone 8的应用程序。 I've decided to use Sqlite PCL in a portable library to cache some data.
我决定在便携式库中使用Sqlite PCL来缓存一些数据。
I didn't find recent information on the internet whether I can use or not linq on a table. 我没有在互联网上找到最近的信息我是否可以在桌面上使用linq。
Of course I can do that 我当然可以这样做
var phones = db.Table<PhoneNumber>().Where(x => some condition).ToList();
If I take a look at the return value of the Where
statement, it is a TableQuery
. 如果我看一下
Where
语句的返回值,它就是一个TableQuery
。
My question is : Do I retrieve all the phone numbers by doing this and then use Linq to filter the items? 我的问题是:我是否通过执行此操作检索所有电话号码,然后使用Linq过滤项目? Or does linq filter the items directly in the sql command before returning them?
或者linq在返回之前直接在sql命令中过滤项目?
To my mind, I wound say that Linq filters the items directly in the sql statement as the Where
function return a TableQuery
but I didn't find any confirmation yet. 在我看来,我伤口说Linq直接在sql语句中过滤项目,因为
Where
函数返回一个TableQuery
但我还没有找到任何确认。
2nd question: Is it the same when I use FirstOrDefault
? 第二个问题:当我使用
FirstOrDefault
时它是一样的吗?
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."
“SQLite.TableQuery有一个'Where'的扩展方法,它采用谓词。”
That means that Linq will modify the sql statement in consequence. 这意味着Linq将修改sql语句。
"But SQLite.TableQuery only has a FirstOrDefault that doesn't take parameters:"
“但SQLite.TableQuery只有一个不带参数的FirstOrDefault:”
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 这意味着,如果你将
FirstOrDefault
与谓词一起使用,它将检索整个表,但如果你在tablequery上没有谓词的情况下使用它,它将修改sql语句
"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."
“如果你使用谓词(我的第二种方法)调用FirstOrDefault,SQLite.NET会选择整个表,然后使用LINQ To Objects对内存中的集合执行FirstOrDefault。”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.