Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here's my error

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

and here's the code that causes it:

model.Courses = db.Courses
    .Where(c => model.Tracks.Any(t => t.Certification.ID == certificate))
    .ToList();

Also, model.Courses is IEnumerable<Courses>, and I'm not sure if that has anything to do with my problem.

Could anybody shed some light on this for me? Many thanks.

share|improve this question
1  
What is model.Tracks? –  Sergey Berezovskiy Jun 12 '13 at 16:21
3  
You are selecting every value of db.Courses: you aren't doing anything with c in the .Where(c => ... clause. Is certificate supposed to be c.certificate? –  klugerama Jun 12 '13 at 16:22
    
@lazyberezovsky IEnumerable<Tracks>. –  shruggernaut Jun 12 '13 at 16:22
    
@klugerama I think that might be my issue, I'll check it out. Thank you –  shruggernaut Jun 12 '13 at 16:25

3 Answers 3

Try using LINQ 'Where In', this should solve your problem:

var names = new string[] { "Alex", "Colin", "Danny", "Diego" };

var matches = from person in people
        where names.Contains(person.Firstname)
        select person;

OR

var matches = from person in people
        where person.Firstname == "Alex" ||
              person.Firstname == "Colin" || 
              person.Firstname == "Danny" ||
              person.Firstname == "Diego"
        select person;

http://blogs.msdn.com/b/alexj/archive/2009/03/26/tip-8-writing-where-in-style-queries-using-linq-to-entities.aspx

share|improve this answer

db.Courses is an IQueryable. While the syntax is virtually identical to the LINQ methods of IEnumerable, under the hood they're completely different.

The IQueryable code isn't actually exectued anywhere at all. It just creates a bunch of Expression objects that different query providers are able to use to do...whatever they want (in this case query a database). Those query providers need to specifically have code to handle any given type of input. There are some things that they either can't sensibly transform into a SQL query, or things that the programmers simply didn't think of or choose to handle (even if it might have a sensible SQL translation).

In sort, the query provider just doesn't know how to translate model.Tracks.Any(t => t.Certification.ID == certificate) into SQL.

You simply need to know what types of code is and isn't supported by the query provider that you're using and try to manipulate the code you have into something that it can handle. Something like this should work:

var certificates = model.Tracks.Select(t => t.Certification.ID).ToList();

model.Courses = db.Courses
    .Where(c => certificates.Contains(c))
    .ToList();

If this were C# code executing all of this in LINQ to objects the two queries would be identical, but to a query provider they're not. They simply have special support for knowing when they see List.Contains to map it to a SQL IN clause. They didn't add specific support for what you did in your first query.

share|improve this answer
up vote 0 down vote accepted

My apologies for the almost unnecessary question, although @klugerama did help me figure it out.

There was a problem with this:

model.Courses = db.Courses
    .Where(c => model.Tracks.Any(t => t.Certification.ID == certificate))
    .ToList();

Until I changed it to this:

model.Courses = db.Courses
    .Where(c => c.Track.Certification.ID == certificate)
    .ToList();

Again, my apologies. Thanks to everyone for their input.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.