Jorge
Jorge

Reputation: 1

I'm getting exception when query with Linq to Entity?

Here is the exception

Unable to create a constant value of type 'System.Collections.Generic.IEnumerable`1'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

Here is my code

List<int> items = new List<int>(){1,5,7,14};

var selecteditems = (from u in _entities.UserInfo
                     join type in items on u.TypeID equals  type
                     where u.UserId == userid
                     select new UserClass
                     {
                         FirstName = u.FirstName,
                         LastName = u.LastName
                     }).ToList();

Any ideas what can cause the exception? and maybe some workaround?

Upvotes: 0

Views: 1471

Answers (1)

Brian Cauthon
Brian Cauthon

Reputation: 5534

For EF4
Get rid of the join on items and add && items.Contains(u.TypeID) to the where clause.

int[] items = new[] { 1, 5, 7, 14 }; 
var selecteditems = (from u in _entities.UserInfo 
                        where u.UserId == userid && items.Contains(u.TypeID)
                        select new UserClass {
                                            FirstName = u.FirstName, 
                                            LastName = u.LastName, 
                                            Email = ul.Email
                                    }).ToList();

For EF1
EF1 does not support collection-valued parameters (i.e. Contains()) This post contains code for a utility method called BuildContainsExpression that can be used to get around this restriction.

UPDATE
I updated the answer to reflect comments from Craig Stuntz regarding solutions for EF1 & EF4

Upvotes: 3

Related Questions