【问题标题】:Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.List<modelClass> [duplicate]无法将类型“System.Collections.Generic.IEnumerable<AnonymousType#1>”隐式转换为“System.Collections.Generic.List<modelClass>”[重复]
【发布时间】:2013-02-12 16:36:07
【问题描述】:

我正在尝试填充 AccountNumber 不存在的交易数据。我需要访问 Account 表来获得它。我在尝试返回 IEnumerable 时收到以下错误

无法将类型 System.Collections.Generic.IEnumerable&lt;AnonymousType#1&gt; 隐式转换为 System.Collections.Generic.List&lt;ProjectModel.Transaction&gt;

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

代码是:

    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;

    }

【问题讨论】:

    标签: c# linq linq-to-sql


    【解决方案1】:

    匿名类型列表不能转换为事务列表。看起来您的 Transaction 类没有 AccountNumber 属性。您也不能从方法返回匿名对象。所以你应该创建一些类型来保存所需的数据:

    public class AccountTransaction
    {
        public int LocalAmount { get; set; }
        public int AccountNumber { get; set; }
    }
    

    并返回这些对象:

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

    顺便说一句你不需要在 where 过滤器中重复加入条件

    【讨论】:

    • 这非常有效。优秀的答案。非常感谢你让我朝着正确的方向前进。这样做我也学到了一些其他的东西。现在我知道 ViewModel 是什么。
    【解决方案2】:

    您在 Linq 查询的“选择新”部分中投影的匿名类型不能直接转换为您的“事务”类型。

    相反,您应该投影一个新的 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();
    

    【讨论】:

    • 这行不通,因为我在 Transaction 表中没有 AccountNumber 属性。
    • 也许下次也列出你的 poco 类的属性? :)
    • 这绝对是个好主意,下次我一定会记住的。不过感谢您的帮助。
    • 没问题 shaz - 抱歉,如果我很刻薄,还没有喝咖啡 :)
    • 一点也不,至少我可以理解我不能使用这段代码,因为该属性不存在。但是当实体具有相似的属性时,我绝对可以做到这一点。所以没有什么是浪费的。
    猜你喜欢
    • 2013-05-14
    • 2011-05-14
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    相关资源
    最近更新 更多