检查 linq 对象是否被引用
检查 linq 对象是否从另一个表引用的最佳(最快)方法是什么。通常我会这样做,但我想这在更大的桌子上可能会很慢。
CurrentObject.ReferencingObjects.Count != 0
这可能会更快。
CurrentObject.ReferencingObjects.FirstOrDefault() != null
有更好的办法吗?
What is the best (fastest) way to check if a linq object is referenced from another table. Normaly i do this way but i guess this might be slow on bigger tables.
CurrentObject.ReferencingObjects.Count != 0
This might be faster.
CurrentObject.ReferencingObjects.FirstOrDefault() != null
Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
ReferencingObjects
实现ICollection
(看起来确实如此,因为它有一个Count
属性),第一个选项实际上可能更快,因为Count
(对于大多数实现)通常是直接存储的,因此这实际上只是直接查找字段的属性。但是,如果您使用
Enumerable.Count()
(方法,而不是属性),那么我的首选方法是使用:作为
Any()
方法非常清楚地表明了您的意图,而且总体上也非常快。If
ReferencingObjects
implementsICollection<T>
(which it appears to, given that it has aCount
property), the first option is likely actually faster, asCount
(for most implementations) is often stored directly, so this effectively is just a property looking up a field directly.If, however, you were using
Enumerable.Count()
(the method, not a property), then my preferred method would instead be to use:As the
Any()
method is very clearly showing your intent, and also very quick in general.