Cannot implicitly convert type 'System.Collections.Generic.List <AnonymousType # 1>' to 'System.Collections.Generic.List <Model.tblLaborBankAccount>'
I have a problem returning a list, here is my code:
public List<tblLaborBankAccount> ListAllLaborBankAccountByLaborID (int laborID)
{
var result = (from b in context.tblBankNames
join c in context.tblLaborBankAccounts
on b.bknBankNameID equals c.bknBankNameID
where c.lbrLaborID == laborID
select (new { c, b })
).ToList();
return result;
}
but it resultreturns an anonymous type, because I am returning a connection request, and the return type is no longer tblLaborBankAccount.
Should I create a new class for this anonymous type or better?
Thank.
+4
2 answers
The select statement is a projection that creates an anonymous class.
Anonymous classes can be used inside a method, but cannot be returned as a result.
You can return List<Tuple<tblBankName, tblLaborBankAccount>>as follows:
public List<Tuple<tblBankName, tblLaborBankAccount>> ListAllLaborBankAccountByLaborID (int laborID)
{
var result = (from b in context.tblBankNames
join c in context.tblLaborBankAccounts
on b.bknBankNameID equals c.bknBankNameID
where c.lbrLaborID == laborID
select (new Tuple<tblBankName, tblLaborBankAccount>( c, b ))
).ToList();
return result;
}
struct/class, :
public class BankAccountDetails
{
public tblBankName BankName {get;set;}
public tblLaborBankAccount BankAccount {get;set;}
}
:
public List<BankAccountDetails> ListAllLaborBankAccountByLaborID (int laborID)
{
var result = (from b in context.tblBankNames
join c in context.tblLaborBankAccounts
on b.bknBankNameID equals c.bknBankNameID
where c.lbrLaborID == laborID
select (new BankAccountDetails{ BankName = c, BankAccount = b })
).ToList();
return result;
}
+3