Miguel Moura
Miguel Moura

Reputation: 39374

EF 5. Intersection performance

I am using Entity Framework 5 and I have the entities: Users, Roles and Files.

I need to check if two list of roles have at least one item in common:

List<Role> a = user.Roles;
List<Role> b = file.Roles;

Boolean commonRoleFound = a.Intersect(b).Count() > 0;

Is there a better way to do this? Maybe using ANY? Maybe other?

What would be the option with better performance?

Thank You, Miguel

Upvotes: 1

Views: 310

Answers (1)

Robert McKee
Robert McKee

Reputation: 21487

In theory "Any" would be faster because you don't need to know how many, just that at least one intersects. It could stop comparing after it found the record that was in both lists.

Upvotes: 1

Related Questions