11

I am using CodeFirst EntityFramework. I have a IQueryable<User> Entities that are returned using context.Users; where context is DbContext of EntityFramework. From this list I have to choose those whose Id is contained in an array of Ids (long). Id is primary key of User entity. I have tried the following but getting compiler error.

IQueryable<User> users = GetQueryableUsers(); 
long [] ids = GetSelectedIds(); //array of long representing Ids key of User entities
users.Intersect(ids); // compilation error
users.Where(user => ids.Contains(user.Id)); //compilation error

Compilation error is (no definition found for Intersect/Contains) Note: System.Linq is already imported.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jatin
  • 4,023
  • 10
  • 60
  • 107

2 Answers2

19

Make sure you are referencing System.Linq

e.g. using System.Linq

Then user.Id must be of type long. You've stated in comments that it is long? because you believed that is how you needed to use the primary key. The solution is to use long and make use of the autogenerate id options of the entity framework.

Alternatively a more general case for non primary keys that could be null would be to use the contains option with the value or default operator.

users.Where(user=>ids.Contains(user.id??0));
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
  • Bob, I have a problem here. The primary key column is db generated, so I have kept it as nullable in my model using long?. Does that mean that I will have to convert long? to long whenever I want to use Linq features like Intersect/Contains ? Thanks for the reply but I have already added using System.Linq. – Jatin Aug 10 '11 at 12:59
  • @Nirvan: A primary key can never be null - how can it be nullable in your model? – BrokenGlass Aug 10 '11 at 13:00
  • I was expecting that database generated keys should be passed as null to SQL Server. If I had not used long? then every time I created a new entity, the key will be set to 0 (the default for long). Can you please let me know how to handle database generated keys in SQL Server and Entity Framework. Actually I am coming from a different platform so a little confused. – Jatin Aug 10 '11 at 13:05
  • @Nirvan: if it's an identity key the DB will generate the key - it will be updated in your model when you save the changes (i.e when adding an entity) with EF - no need to use nullable keys at all. – BrokenGlass Aug 10 '11 at 13:14
  • @BrokenGlass: Good that I came to know about it at an early stage. I think I will change my model and make the key non-nullable. Thanks for that piece of advice. – Jatin Aug 10 '11 at 13:17
  • BrokenGlass is correct, you need to use the autogenerate options of the framework. However if for some reason you need to keep the null then `users.Where(user => ids.Contains(user.Id??0));` will do the job – Bob Vale Aug 10 '11 at 13:18
  • @Bob, Got that. As I said in my earlier comment, I will change the key to long instead of long?. Thanks. – Jatin Aug 10 '11 at 13:36
-1

Your problem is you can't intersect users on long ids. Intersect can only be used on IEnumerables of the same type.

You should use user.Id.GetValueOrDefault() because your ID is long? instead of long.

thekip
  • 3,660
  • 2
  • 21
  • 41
  • You can't intersect a collection of users and a collection of longs either, he needs the Where/Contains clause – Bob Vale Aug 10 '11 at 13:25