jaykzoo
jaykzoo

Reputation: 77

Unable to create constant value of type

i'm receiving this error in the query below. I've searched other similar links, but cant get this to work:

Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context.

var users = from a in GetUsers(EmployeeID)
                group new { a } by new { a.EmployeeID } into g
                select new UserMovement { EmployeeID =  g.Key.EmployeeID };


    var resulttst = from s in _repository.GenericRepository<STG_WDUsers>().GetAll()
                  join u in users on s.Employee_ID equals u.EmployeeID
                  where s.EffectiveDate != null && s.EffectiveDate > dtSixMonths
                  group new { s } by new { s.Employee_ID, s.Business_Unit, s.First_Name, s.Last_Name } into g
                  orderby g.Key.Employee_ID, g.Min(m => m.s.EffectiveDate)
                  select new UserMovement
                  {
                      EffectiveDate = g.Min(m => m.s.EffectiveDate),
                      EmployeeID = g.Key.Employee_ID,
                      FirstName = g.Key.First_Name,
                      LastName = g.Key.Last_Name,
                      BusinessUnit = g.Key.Business_Unit,
                      PreviousBU = null
                  };

Upvotes: 2

Views: 919

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109281

You're getting this error because you use a local sequence of objects, users, in a LINQ query that is translated into SQL. There is no translation for UserMovement objects though. You can use local sequences in LINQ to a SQL backend, but only if they contain primitive values:

var empIds = (from a in GetUsers(EmployeeID)
              select a.EmployeeID).Distinct();
from s in _repository.GenericRepository<STG_WDUsers>().GetAll()
                     .Where(u => empIds.Contains(u.Employee_ID));

Upvotes: 2

Related Questions