检查 linq 对象是否被引用

发布于 2024-12-28 01:27:42 字数 251 浏览 4 评论 0原文

检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上得阳光 2025-01-04 01:27:42

如果 ReferencingObjects 实现 ICollection(看起来确实如此,因为它有一个 Count 属性),第一个选项实际上可能更快,因为 Count (对于大多数实现)通常是直接存储的,因此这实际上只是直接查找字段的属性。

但是,如果您使用 Enumerable.Count() (方法,而不是属性),那么我的首选方法是使用:

CurrentObject.ReferencingObjects.Any();

作为 Any()方法非常清楚地表明了您的意图,而且总体上也非常快。

If ReferencingObjects implements ICollection<T> (which it appears to, given that it has a Count property), the first option is likely actually faster, as Count (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:

CurrentObject.ReferencingObjects.Any();

As the Any() method is very clearly showing your intent, and also very quick in general.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文