1

linqオブジェクトが別のテーブルから参照されているかどうかを確認するための最良の(最速の)方法は何ですか。通常、私はこの方法を実行しますが、大きなテーブルではこれが遅くなる可能性があると思います。

CurrentObject.ReferencingObjects.Count != 0

これはもっと速いかもしれません。

CurrentObject.ReferencingObjects.FirstOrDefault() != null

もっと良い方法はありますか?

4

1 に答える 1

2

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.

于 2012-01-20T20:24:46.747 に答える