简体   繁体   English

不确定我在做什么错得到错误:无法将类型'System.Linq.IQueryable'1强制转换为类型'System.Linq.IQueryable'1'

[英]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.

相关问题 System.Linq.IQueryable错误 - System.Linq.IQueryable Error 无法将类型为“ System.Collections.Generic.List`1 []”的对象强制转换为类型为“ System.Linq.IQueryable`1 []”的对象 - Unable to cast object of type 'System.Collections.Generic.List`1[]' to type 'System.Linq.IQueryable`1[]' 无法将“System.Linq.IQueryable”中的匿名类型转换为“System.Data.Entity.Core.Objects.ObjectQuery” - Unable to cast Anonymous Type from 'System.Linq.IQueryable' to 'System.Data.Entity.Core.Objects.ObjectQuery' 无法隐式转换类型&#39;System.Linq.IQueryable <AnonymousType#1> &#39;到&#39;System.Linq.IQueryable &lt;&gt;&#39; - Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to 'System.Linq.IQueryable<>' 从类型&#39;System.Linq.IQueryable转换时出错 <ClassX> &#39;到&#39;ClassX&#39; - An error for converting from type 'System.Linq.IQueryable<ClassX>' to 'ClassX' 与System.Linq.IQueryable的联合获取错误 - Union with System.Linq.IQueryable get errors 无法将类型'System.Linq.IQueryable <int>'隐式转换为'int?' - Cannot implicitly convert type 'System.Linq.IQueryable<int>' to 'int?' 类型&#39;System.Linq.IQueryable`1 <T0> &#39;在未引用的程序集中定义 - The type 'System.Linq.IQueryable`1<T0>' is defined in an assembly that is not referenced 无法转换类型&#39;System.Linq.IQueryable <double?> &#39; 漂浮&#39; - Cannot convert type 'System.Linq.IQueryable<double?>' to 'float' 无法隐式转换System.Linq.IQueryable类型 <string> 串起来 - Cannot implicitly convert type System.Linq.IQueryable<string> to string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM