[英]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: 在LINQ / C#中,但是似乎会产生以下错误:
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 !!! 零意义,我只想在WHERE IN上运行,但似乎遇到了这个毫无意义的障碍!
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();
select new { q.ConversationId }
creates an anonymously typed object with a property ConversationId. select new { q.ConversationId }
创建一个具有属性ConversationId的匿名类型的对象。 The code creates an IQueryable<[anonymous type]> instead of an IQueryable< int>. 该代码创建一个IQueryable <[匿名类型]>而不是IQueryable <int>。
simply select q.ConversationId
to get an IQueryable< int>. 只需
select q.ConversationId
即可获取IQueryable <int>。
I think it's because the return type of your subquery isn't IQueryable<int>
but IQueryable<anonymousType>
. 我认为这是因为子查询的返回类型不是
IQueryable<int>
而是IQueryable<anonymousType>
。 Changing the select from select new { q.ConversationId }
to select q.ConversationID
should resolve (untested). 将选择从
select new { q.ConversationId }
更改为select q.ConversationID
解决(未测试)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.