当前位置: 首页 > 编程语言 > C#

无法将类型’System.Linq.IQueryable’隐式转换为’System.Collections.Generic.IList’分享

时间:2023-04-10 15:42:56 C#

C#学习教程:无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.IList'Generic.IList'我有一个方法:publicDzieckoAndOpiekunCollectionGetChildAndOpiekunByFirstnameLastname(stringfirstname,stringlastname){DzieckoAndOpiekunCollection结果=newDzieckoAndOpiekunCollection();if(firstname==null&&lastname!=null){IListresultV=frompindb.Dzieckowherelastname==p.Nazwisko**select**newDzieckoAndOpiekun(p.Imie,p.Nazwisko,p.Opiekun.Imie复制代码,p.Opiekun.Nazwisko);结果。添加范围(结果V);}返回结果;}和选定位置的错误:错误1??无法将类型“System.Linq.IQueryable”隐式转换为“System.Collections.Generic.IList”。有一个明确的演员表(你错过了演员表吗?)知道如何解决我的问题吗?要将IQuerable或IEnumerable转换为列表,您可以执行以下操作之一:IQueryableq=...;列表l=q.ToList();或者:IQueryableq=...;列表l=新列表(q);您可以将IListresultVvarresultV替换为varresultV。如果使用where子句,并且不需要IQueryable对象,请确保包含.First()。试试这个->newDzieckoAndOpiekun(p.Imie,p.Nazwisko,p.Opiekun.Imie,p.Opiekun.Nazwisko).ToList()你可以使用.ToList()方法将返回的ILueryable结果转换为IList如下所示,在linq查询之后。publicDzieckoAndOpiekunCollectionGetChildAndOpiekunByFirstnameLastname(字符串名字,字符串姓氏){DataTransfer.ChargeInSchoolEntitiesdb=newDataTransfer.ChargeInSchoolEntities();DzieckoAndOpiekunCollection结果=newDzieckoAndOpiekunCollection();if(firstname==null&&lastname!=null){IListresultV=frompindb.Dzieckowherelastname==p.Nazwisko**select**newDzieckoAndOpiekun(p.Imie,p.Nazwisko,p.Opiekun.Imie复制代码,p.Opiekun.Nazwisko).ToList();结果.AddRange(resultV);}返回结果;我已经通过这种方式解决了我在db上的记录列表中的问题以上是C#学习教程:Cannotimplicitlyconverttype'System.如果IList'分享的所有内容对你有用,需要进一步了解C#学习教程,希望你多多关注—ListOfObjectToBeOrder.OrderBy(x=>x.columnName).ToList();代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: