这个linq查询效率高吗?
这个linq查询效率高吗?
var qry = ((from member in this.ObjectContext.TreeMembers.Where(m => m.UserId == userId && m.Birthdate == null)
select member.TreeMemberId).Except(from item in this.ObjectContext.FamilyEvents select item.TreeMemberId));
var mainQry = from mainMember in this.ObjectContext.TreeMembers
where qry.Contains(mainMember.TreeMemberId)
select mainMember;
这会被转换为多个 sql 调用还是仅一个?可以优化吗?基本上我有 2 个表,我想从 table1 中选择那些日期时间为空的记录,并且该记录不应存在于 table2 中。
Is this linq query efficient?
var qry = ((from member in this.ObjectContext.TreeMembers.Where(m => m.UserId == userId && m.Birthdate == null)
select member.TreeMemberId).Except(from item in this.ObjectContext.FamilyEvents select item.TreeMemberId));
var mainQry = from mainMember in this.ObjectContext.TreeMembers
where qry.Contains(mainMember.TreeMemberId)
select mainMember;
Will this be translated into multiple sql calls or just one? Can it be optimised? Basically I have 2 tables, I want to select those records from table1 where datetime is null and that record should not exist in table2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确定查询是否会进行多次调用的最简单方法是设置数据上下文的
.Log
属性。我通常将其设置为写入DebugOutputWriter
。可以找到此类类的一个很好的示例 此处。然而,对于一般的思考方式来说,如果您使用的类的属性不直接映射到 where 子句或 join 子句中的数据库字段,那么它通常会进行多次调用。从您提供的内容来看,您的情况似乎并非,但我不能绝对确定并建议使用上面列出的方法。
The easiest way to find out if the query will make multiple calls is to set the
.Log
property of the data context. I typically set it to write to aDebugOutputWriter
. A good example for this kind of class can be found here.For a general way of thinking about it however, if you use a property of your class that does not directly map to a database field in a where clause or a join clause, it will typically make multiple calls. From what you have provided, it looks like this is not the case for your scenario, but I can't absolutely certain and suggest using the method listed above.