LINQ:何时使用编译查询?
我想就此得到一些专家的建议。我以前使用过编译查询,但对于这种特殊情况,我不确定它是否合适。
这是一个搜索表单,其中查询会发生变化,并且取决于搜索的内容。
static Func<DBContext, int, IQueryable<Foo>> Search = CompiledQuery.Compile(
(DBContext db, int ID) =>
db.Person
.Where(w => w.LocationID = ID)
.Select(s =>
new Foo
{
Name = s.PersonName,
Age = s.Age,
Location = s.LocationName,
Kin = s.Kin
}));
现在,如果有人填写搜索框,我想通过向查询添加另一个 Where
语句来扩展查询:
var query = Search(context, 123);
query = query.Where(w => w.Name.Contains(searchString));
所以我的问题是,它是否返回 LocationID == 123 的所有结果
,然后检查 searchString
匹配的结果?或者它实际上扩展了编译的查询?
如果是前者(我怀疑是这样),是否应该废弃 CompiledQuery 并只创建一个扩展查询的方法,然后将其作为列表返回?
另外,CompiledQuery
使用的最佳实践是什么?是否有何时使用它们的指南?
注意:我在带有 Linq to SQL 的 ASP.NET 网站中使用上述内容。不确定这是否有什么区别。
谢谢
I'd like some expert advice on this. I've used compiled queries before, but for this particular case, i'm not sure whether it's appropriate.
It's a search form where the query changes and is dependent on what is being searched on.
static Func<DBContext, int, IQueryable<Foo>> Search = CompiledQuery.Compile(
(DBContext db, int ID) =>
db.Person
.Where(w => w.LocationID = ID)
.Select(s =>
new Foo
{
Name = s.PersonName,
Age = s.Age,
Location = s.LocationName,
Kin = s.Kin
}));
Now if someone fills in the search box, i want to extend the query by adding another Where
statement to the query:
var query = Search(context, 123);
query = query.Where(w => w.Name.Contains(searchString));
So my question is, is it returning all the results where LocationID == 123
, then checking the results for a searchString
match? Or is it actually extending the compiled query?
If it's the former (which i suspect it is), should scrap the CompiledQuery
and just create a method that extends the query then return it as a list?
Also, what are the best practices for CompiledQuery
usage and is there a guideline of when they should be used?
Note: I'm using the above in an ASP.NET website with Linq to SQL. Not sure if that makes any difference.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是编译后的查询是一成不变的;它知道将对数据库运行什么 SQL。然而,lambda 表达式是延迟加载的,并且无法修改编译查询,因为它是在运行时运行的。坏消息是,它将返回数据库中的所有记录,但它将查询内存中的这些记录以进一步细化它们。
如果您想编译查询,那么我建议编写两个具有不同签名的查询。
The problem is that the compiled query is set in stone; it knows what SQL it will run against the database. The lambda expression is lazy loaded however, and cannot modify the compile query as it is being run during run time. The bad news is that it will return all of the records from the database, but it will query those records in memory to further refine them.
If you want to compile the query then I would suggest writing two queries with different signatures.
据我所知,编译查询一次是一个很好的做法,这就是预编译查询的全部意义(这就是为什么预编译查询是静态的),它节省了将该查询编译为 SQL 的时间。如果它扩展了预编译的查询,那么它会再次编译该查询,这会导致您失去收益。
结果上的查询结果(您的查询变量)不再是 LINQ to SQL。
As far as I know, it is good practice to compile your query once, that is the whole point of pre-compiled query(and that's why your pre-compiled query is static), it saves time to compile that query into SQL. If it extend that pre-compiled query, then it is compiling that query again, which you loose gains.
Query result on result (your query variable) is no longer LINQ to SQL.
只需在编译的查询中包含附加条件即可。
Just include your additional condition in your compiled query.
如果我是对的那么你需要 linq 中的一些动态 where 子句。因此,我建议走这条路
,希望你能明白我的话。
If i am right then you need some dynamic where clause in linq. So for that i would suggest go this way
I hope you got my words.