C# 无法将类型“System.Collections.Generic.IEnumerable<AnonymousType#1>”隐式转换为“System.Collections.Generic.List<modelClass>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14837302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.List<modelClass>
提问by shaz
I am trying to populate Transaction data where AccountNumber does not exist. I need to access the Account table to get that. I am getting the following error where I am trying to return IEnumerable
我正在尝试填充 AccountNumber 不存在的交易数据。我需要访问 Account 表才能得到它。我在尝试返回 IEnumerable 时遇到以下错误
Cannot implicitly convert type System.Collections.Generic.IEnumerable<AnonymousType#1>
to System.Collections.Generic.List<ProjectModel.Transaction>
不能将类型隐式转换System.Collections.Generic.IEnumerable<AnonymousType#1>
为System.Collections.Generic.List<ProjectModel.Transaction>
The error is shown on top of .ToList();part of the code. What am I doing wrong?
错误显示在.ToList();之上。代码的一部分。我究竟做错了什么?
the code is:
代码是:
public static IEnumerable<Transaction>GetAllTransactions()
{
List<Transaction> allTransactions = new List<Transaction>();
using (var context = new CostReportEntities())
{
allTransactions = (from t in context.Transactions
join acc in context.Accounts on t.AccountID equals acc.AccountID
where t.AccountID == acc.AccountID
select new
{
acc.AccountNumber,
t.LocalAmount
}).ToList();
}
return allTransactions;
}
采纳答案by Sergey Berezovskiy
List of anonymous types cannot be casted to list of transactions. Looks like your Transaction
class do not have AccountNumber
property. Also you cannot return anonymous objects from methods. So you should create some type which will hold required data:
匿名类型列表不能转换为交易列表。看起来你的Transaction
班级没有AccountNumber
财产。你也不能从方法中返回匿名对象。所以你应该创建一些类型来保存所需的数据:
public class AccountTransaction
{
public int LocalAmount { get; set; }
public int AccountNumber { get; set; }
}
And return these objects:
并返回这些对象:
public static IEnumerable<AccountTransaction> GetAllTransactions()
{
using (var context = new CostReportEntities())
{
return (from t in context.Transactions
join acc in context.Accounts
on t.AccountID equals acc.AccountID
select new AccountTransaction {
AccountNumber = acc.AccountNumber,
LocalAmount = t.LocalAmount
}).ToList();
}
}
BTWyou don't need duplicate join condition in where filter
顺便说一句,您不需要 where 过滤器中的重复连接条件
回答by RainbowFish
The anonymous type which you're projecting in the "select new" section of your Linq query cannot be casted directly to your "Transaction" type.
您在 Linq 查询的“选择新”部分中投影的匿名类型无法直接转换为您的“交易”类型。
Instead, you should project a new instance of Transaction. The following might help:
相反,您应该投影一个新的 Transaction 实例。以下可能有帮助:
allTransactions = (from t in context.Transactions
join acc in context.Accounts on t.AccountID equals acc.AccountID
where t.AccountID == acc.AccountID
select new Transaction()
{
AccountNumber = acc.AccountNumber,
LocalAmount = t.LocalAmount
}).ToList();