0

I am getting a list of my database, selecting certain things, then i want to query another database where the original list contains this databases job reference

 var jobRefs = context.jobs.Where(j => j.LSM_Status == null &&
                                       j.despatched_time == null
                                       )
      .Select(x => new { x.job_ref, x.volumes_env, x.postout_deadline , x.UniqNo })
      .ToList();


var UpdatedRefs = context.customerslas.Where(c => jobRefs.Any(z=>z.job_ref == c.job_ref) &&
                                      (c.invoiced == 1 ||
                                       c.invoiced == 2) &&
                                       c.active == 1)
                                       .Select(c => c.job_ref)
                                       .ToList();

And get this error

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

Ben Bodie
  • 75
  • 12

1 Answers1

1

The ToList() in the first first query is fetching data to a collection in memory while the second query, where you compare the data, is in database. To solve this you need to make them in the same area, either db or memory.

Easiest way, and recommended, would be to just remove ToList() from the first query

var jobRefs = context.jobs.Where(j => j.LSM_Status == null &&
                                       j.despatched_time == null
                                       )
      .Select(x => new { x.job_ref, x.volumes_env, x.postout_deadline , x.UniqNo });


var UpdatedRefs = context.customerslas.Where(c => jobRefs.Any(z=>z.job_ref == c.job_ref) &&
                                      (c.invoiced == 1 ||
                                       c.invoiced == 2) &&
                                       c.active == 1)
                                       .Select(c => c.job_ref)
                                       .ToList();
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69