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

无法将类型’System.Collections.Generic.IEnumerable ‘隐式转换为’System.Collections.Generic.List分享

时间:2023-04-10 12:07:15 C#

C#学习教程:Unabletoimplicitlyconverttype'System.Collections.Generic.IEnumerable'to'System.Collections.Generic.ListGeneric.List我正在尝试填充AccountNumber不存在的交易数据。我需要访问帐户表才能获取它。我收到以下错误,我正在尝试返回IEnumerable无法将类型System.Collections.Generic.IEnumerable隐式转换为System.Collections.Generic.List该错误显示在.ToList();代码的一部分。我究竟做错了什么?代码是:publicstaticIEnumerableGetAllTransactions(){ListallTransactions=newList();使用(varcontext=newCostReportEntities()){allTransactions=(fromtincontext.Transactionsjoinaccincontext.Accountsont.AccountIDequalsacc.AccountIDwheret.AccountID==acc.AccountIDselectnew{acc.AccountNumber,t.LocalAmount}).ToList();}返回所有交易;}无法将匿名类型列表转换为事务列表。看起来您的Transaction类没有AccountNumber属性。此外,您不能从方法返回匿名对象。所以你应该创建一些类型来保存所需的数据:publicclassAccountTransaction{publicintLocalAmount{get;放;}publicintAccountNumber{得到;放;}}并返回这些对象:publicstaticIEnumerableGetAllTransactions(){using(varcontext=newCostReportEntities()){return(fromtincontext.Transactionsjoinaccincontext.Accountsont.AccountIDequalsacc.AccountIDselectnewAccountTransaction{AccountNumber=acc.AccountNumber,LocalAmount=t.LocalAmount}).ToList();}}顺便说一下,你不需要filter中的duplicatejoin条件你在Linq查询的“selectnew”部分预测的匿名类型不能直接转换为“transaction”类型。相反,您应该投影一个新的Transaction实例。以下内容可能会有所帮助:以上是C#学习教程:类型'System.Collections.Generic.IEnumerable'不能隐式转换为'System.Collections.Generic.List。详细了解C#学习教程,希望大家多多关注——allTransactions=(fromtincontext.Transactionsjoinaccincontext.Accountsont.AccountIDequalsacc.AccountIDwheret.AccountID==acc.AccountIDselectnewTransaction(){AccountNumber=acc.AccountNumber,LocalAmount=t.LocalAmount}).ToList();本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: