Eveytime I think I am getting a handle on this stuff.
Full Error:
Unable to create a constant value of type ‘Models.staffnames’. Only primitive types (‘such as Int32, String, and Guid’) are supported in this context.
Versions: .Net 4 EF 4
I think it has something to do with DU and db.UserProfiles being Enumerable but when I try to push it back to make everything IQueryable the whole thing blows up. As it stands right now the error occurs when building the supportlist.
It is interesting to note that the only time this problem seems to crop up is when I am working with the Roles provider. Which in the past I just dumped it out into a for loop.
Any help would be appreciated.
Code:
IEnumerable<MembershipUser> du = Roles.GetUsersInRole("Dealer").Select(u => Membership.GetUser(u)).AsEnumerable();
IQueryable<staffnames> du_up = du.Join(db.UserProfiles.AsEnumerable(), mu => (Guid)mu.ProviderUserKey, up => up.UserID, (mu, up) => new staffnames
{
UserId = (Guid)mu.ProviderUserKey,
FirstName = up.FirstName,
LastName = up.LastName
}).AsQueryable();
List<MemberSupportModel> support = db.Wins_support.Where(s=>s.dealerid == uid && !s.closed).Select(s=> new MemberSupportModel{
aeramanagerack = s.aeramanagerack,
amgrname = du_up.Where(a => a.UserId == s.aeramanger).Select(a=>a.FirstName+" "+a.LastName).FirstOrDefault(),
aeramangerackdate = s.aeramangerackdate,
atypedesc = _atype.Where(wt => wt.Value == s.atypeid).Select(wt => wt.Description).FirstOrDefault(),
wtypedesc = _wtype.Where(wt => wt.Value == s.typeid).Select(wt => wt.Description).FirstOrDefault(),
comment =s.comment,
dateSubmitted = s.dateSubmitted,
vdate =s.vdate,
venuename = s.Venues_Logs.Venue.venueName
}).ToList();
The problem is the line
amgrname = du_up.Where(...becausedu_upis a collection of a non-primitive type in memory, hence the exception.AsQueryabledoesn’t help here, in-memory is in-memory, you can’t change that.I would try to load the data you need from the DB separately and then merge your
du_upcollection with the collection of data loaded from the DB: