1

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?

khr055
  • 28,690
  • 16
  • 36
  • 48
Andreas
  • 6,958
  • 10
  • 38
  • 52

1 Answers1

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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373