IQueryable什么时候执行查询?
我从网站示例中得到了以下代码。它工作得很好,但我不太明白它是如何工作的...
public static Func<EuvaTransientDataContext, string, string, int, IQueryable<SecurityAudit>>
MatchedIPAddressesAuditRecords =
CompiledQuery.Compile((EuvaTransientDataContext db, string username, string ipAddress, int withinHours) =>
(from sa in db.SecurityAudits
where sa.IPAddress == ipAddress &&
sa.Username != username &&
sa.AuditDateTime >= DateTime.Now.AddHours(withinHours * -1)
select sa));
我很欣赏代码有点具体,但我认为发生的情况如下:
- 我正在创建一个接受多个参数的委托,并且返回输入到 SecurityAudit 的 IQuerable。
- 我正在创建一个已编译的查询等。
我现在可以通过执行类似的操作来使用它(抱歉,我手头没有准确代码)...
IList<SecurityAudit> = someDataContext
.MatchedIPAddressesAuditRecords("username", "ipaddress", 24)
.ToList<SecurityAudit>();
我不明白的是 IQueryable 在这里是如何工作的?
- 我是否将查询接口返回到我的调用方法?
- 我的编译查询存储在哪里以及何时执行?
- 返回接口 IQueryable 有何相关性?
如果能解释一下它实际上是如何工作的,那就太好了。
谢谢。
Ive got the following code which I hacked together from website examples. It works nicely but I dont really understand how it works...
public static Func<EuvaTransientDataContext, string, string, int, IQueryable<SecurityAudit>>
MatchedIPAddressesAuditRecords =
CompiledQuery.Compile((EuvaTransientDataContext db, string username, string ipAddress, int withinHours) =>
(from sa in db.SecurityAudits
where sa.IPAddress == ipAddress &&
sa.Username != username &&
sa.AuditDateTime >= DateTime.Now.AddHours(withinHours * -1)
select sa));
I appreciate the code is a bit specific but what I think is happening as follows:
- I am creating a delegate that accepts a number of paremeters and returns an IQuerable typed to SecurityAudit.
- I am creating a compiled query etc.
I can now consume this by doing somethign like this (sorry I dont have the exact code to hand)...
IList<SecurityAudit> = someDataContext
.MatchedIPAddressesAuditRecords("username", "ipaddress", 24)
.ToList<SecurityAudit>();
What I dont understand is how the IQueryable is working here?
- Am I returning an interface to a query to my calling method?
- Where is my compiled query stored and when is it executed?
- What is the relevance of returning an interface IQueryable?
Would be greatful for some explantion on how this is actually working.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CompiledQuery.Compile 在静态构造函数中调用一次。
此方法创建 CompiledQuery 的实例,将查询保存在此实例中,并返回将由用户代码调用的运行时方法的引用。
当用户执行该方法时,将编译查询(仅第一次)并执行该方法。
CompiledQuery.Compile is called once in the static constructor.
This method creates an instance of CompiledQuery, saves the query in this instance and returns a reference on its runtime method that will be called by user code.
When user executes the method the query is compiled (for the first time only) and the method is executed.