davethecoder
davethecoder

Reputation: 3932

Not sure what i am doing wrong get error: Unable to cast the type 'System.Linq.IQueryable`1' to type 'System.Linq.IQueryable`1'

i have this query:

var model2 = (from p in context.ViewChatPeoples
                           where ((IQueryable<int>)(from q in context.ConversationPeoples
                           where !q.Deleted && q.PersonId == info.LoginID
                           select new { q.ConversationId })).Contains(p.ConversationId)
                      select p.ConversationId).Distinct().ToList();

in LINQ / C#, however it seems to produce the following error:

Unable to cast the type 'System.Linq.IQueryable`1' to type 'System.Linq.IQueryable`1'. LINQ to Entities only supports casting Entity Data Model primitive types.

makes zero sense, i just want to run an WHERE IN, but seem to have hit this hurdle that makes no sense what so ever !!!

thanks

and as an update here is the final working code using the given solution:

var model2 = (from p in context.ViewChatPeoples
                           where ((from q in context.ConversationPeoples
                           where !q.Deleted && q.PersonId == info.LoginID
                           select q.ConversationId)).Contains(p.ConversationId)
                      select p.ConversationId).Distinct().ToList();

Upvotes: 0

Views: 898

Answers (2)

Dave
Dave

Reputation: 3621

I think it's because the return type of your subquery isn't IQueryable<int> but IQueryable<anonymousType>. Changing the select from select new { q.ConversationId } to select q.ConversationID should resolve (untested).

Upvotes: 3

devio
devio

Reputation: 37205

select new { q.ConversationId } creates an anonymously typed object with a property ConversationId. The code creates an IQueryable<[anonymous type]> instead of an IQueryable< int>.

simply select q.ConversationId to get an IQueryable< int>.

Upvotes: 4

Related Questions