Silagy
Silagy

Reputation: 3083

Getting unable to create a constant value of type Linq error

I am trying to get data from MSSQL database throughout EF.

See the code below:

model.events =
            _FoxDB.DWH_EventsBasicData.Where(
                e => e.Event_StartTime >= model.StartDate && e.Event_EndTime <= model.EndDate).ToList();

model.Tps = _FoxDB.DWH_TrainingProgramsBasicData.Where(
                t => model.events.Any(e => e.EventTrainingProgram_ID.Equals(t.TrainingProgramID))).Select(t => new EntityDropDown()
                {
                    ID = t.TrainingProgramID,
                    Name = t.TrainingProgramName
                }).ToList();

In the first line i am getting list of events > that part works just fine, i also convert it into ToList so it will run locally.

Than i want to get a list of Training Programs that exist in the event collection.

I read quite few answers regarding this error and article and i still don't understand the problem.

The error: Unable to create a constant value of type 'FoxConcept.Models.DALModels.DWH_EventBasicData'. Only primitive types or enumeration types are supported in this context

Any help appreciated.

Upvotes: 1

Views: 48

Answers (1)

Adil Mammadov
Adil Mammadov

Reputation: 8676

As far as I know this error is because you load model.events to memory and then try to use it at the query to the database. I am not sure but it should work for you:

model.Tps = _FoxDB.DWH_TrainingProgramsBasicData
                .AsEnumerable() // I added this
                .Where(t => 
                    model.events
                        .Any(e => e.EventTrainingProgram_ID.Equals(t.TrainingProgramID)))
                        .Select(t => new EntityDropDown()
                        {
                            ID = t.TrainingProgramID,
                            Name = t.TrainingProgramName
                        }).ToList();

Upvotes: 1

Related Questions