LINQ Join Query Structure Resulting in CS0119 Error

Viewed 194

I'm trying to join two different class models in an MVC project together so I can order them ascending/descending. I've tried several permutations but can't seem to get the LINQ query to play nice. Any suggestions on what I'm doing wrong?

var lateContact = from c in JPLatestContact
                  join s in JPStudent on c.ApplicationUserId equals 
                  s.ApplicationUserId
                  orderby c.JPLatestContactDate ascending
                  select s;

I'm a beginner when it comes to this, but if I'm understanding this correctly, the "c" and "s" are variables I make up myself. "JPLatestContact" and "JPStudent" are the two models/classes/tables I want to join, and both have "ApplicationUserId" that I can join them on, and I want to order all the results by the value "JPLatestContactDate" found in the JPLatestContact model, in ascending order.

With the query I've written above, I'm getting a CS0119 error "'JPLatestContact' is a type, which is not valid in the given context."

I'm not sure where I'm going wrong with my structure, or have I misused the JOIN structure in some way?

1 Answers

You cannot run a LINQ select on a type, only on a collection of that type - i.e. anything that implements IEnumerable<JPLatestContact> or IQueryable<JPLatestContact>, such as List<JPLatestContact>, dbContext.JPLatestContact, etc. Same goes for JPStudent - you need a collection or IQueryable<JPStudent> for it.

Assuming that you are querying EF, the query should look like this:

var lateContact = from c in dbContext.JPLatestContact
                  join s in dbContext.JPStudent on c.ApplicationUserId equals 
                  s.ApplicationUserId
                  orderby c.JPLatestContactDate ascending
                  select s;

Make sure that all entity names and property names match the actual names as defined in your EF model.

Related