无法隐式转换类型

无法隐式转换类型

本文介绍了无法隐式转换类型'System.Collections.Generic.IEnumerable< AnonymousType#1>'以“System.Collections.Generic.List< modelClass>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来填充交易数据,其中账户号码不存在。我需要访问帐户表来获取。我收到以下错误在哪里,我试图返回的IEnumerable



无法隐式转换类型 System.Collections.Generic.IEnumerable< AnonymousType#1> System.Collections.Generic.List< ProjectModel.Transaction>



错误显示在 .ToList()的顶部; 部分代码。我究竟做错了什么?



代码:

 公共静态的IEnumerable<交易> GetAllTransactions ()
{
名单,LT;交易> allTransactions =新的List<&交易GT;();
使用(VAR上下文=新CostReportEntities())
{
allTransactions =(从吨context.Transactions
在t.AccountID等于acc.AccountID context.Accounts加入ACC
,其中t.AccountID == acc.AccountID
选择新的
{
acc.AccountNumber,
t.LocalAmount
})了ToList()。

}
返回allTransactions;

}


解决方案

一览匿名类型不能铸造的交易的列表。看起来像你的交易类没有账户号码属性。你也无法从方法返回匿名对象。所以,你应该创建一些类型,将保存所需的数据:

 公共类AccountTransaction 
{
公众诠释LocalAmount {搞定;组; }
公众诠释账户号码{搞定;组; }
}

和返回这些对象:

 公共静态的IEnumerable< AccountTransaction> GetAllTransactions()
{
使用(VAR上下文=新CostReportEntities())
{
在context.Transactions
收益率(从T加入ACC在context.Accounts $在t.AccountID b $ b等于acc.AccountID
选择新AccountTransaction {
账户号码= acc.AccountNumber,
LocalAmount = t.LocalAmount
})了ToList()。
}
}

BTW 的你不吨需要重复的加入在过滤器


条件

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

Cannot implicitly convert type System.Collections.Generic.IEnumerable<AnonymousType#1> to System.Collections.Generic.List<ProjectModel.Transaction>

The error is shown on top of .ToList(); part of the code. What am I doing wrong?

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;

    }
解决方案

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:

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();
    }
}

BTW you don't need duplicate join condition in where filter

这篇关于无法隐式转换类型'System.Collections.Generic.IEnumerable&LT; AnonymousType#1&GT;'以“System.Collections.Generic.List&LT; modelClass&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:31