c# - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.List<modelClass> -
i trying populate transaction info accountnumber not exist. need access business relationship table that. getting next error trying homecoming ienumerable
cannot implicitly convert type system.collections.generic.ienumerable<anonymoustype#1>
system.collections.generic.list<projectmodel.transaction>
the error shown on top of .tolist(); part of code. 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 bring together acc in context.accounts on t.accountid equals acc.accountid t.accountid == acc.accountid select new { acc.accountnumber, t.localamount }).tolist(); } homecoming alltransactions; }
list of anonymous types cannot casted list of transactions. looks transaction
class not have accountnumber
property. cannot homecoming anonymous objects methods. should create type hold required data:
public class accounttransaction { public int localamount { get; set; } public int accountnumber { get; set; } }
and homecoming these objects:
public static ienumerable<accounttransaction> getalltransactions() { using (var context = new costreportentities()) { homecoming (from t in context.transactions bring together acc in context.accounts on t.accountid equals acc.accountid select new accounttransaction { accountnumber = acc.accountnumber, localamount = t.localamount }).tolist(); } }
btw don't need duplicate bring together status in filter
c# linq linq-to-sql
No comments:
Post a Comment