C# 将对象列表复制到另一个对象中的另一个对象列表
我的方法返回一个包含对象列表的响应对象C# 将对象列表复制到另一个对象中的另一个对象列表,c#,linq,linq-to-objects,C#,Linq,Linq To Objects,我的方法返回一个包含对象列表的响应对象 public class GetAccountsListResponse { public List<AccountsList> Accounts { get; set; } } public class AccountsList { public string AccountId { get; set; } public string AccountName { get; set; } } 公共类GetAccou
public class GetAccountsListResponse
{
public List<AccountsList> Accounts { get; set; }
}
public class AccountsList
{
public string AccountId { get; set; }
public string AccountName { get; set; }
}
公共类GetAccountsResponse
{
公共列表帐户{get;set;}
}
公共类帐户列表
{
公共字符串AccountId{get;set;}
公共字符串AccountName{get;set;}
}
我尝试了类似的方法,但我遇到了null reference异常:
public GetAccountsListResponse GetAccountsList()
{
AccountRepository objRepo = new AccountRepository();
GetAccountsListResponse res = new GetAccountsListResponse();
List<Account> lstAccnts = new List<Account>();
lstAccnts = objRepo.GetAccountsFromRepo();
foreach (var item in lstAccnts)
{
res.Accounts = new List<AccountsList>();
res.Accounts.ForEach(c => c.AccountId = item.AccountId);
}
return res;
}
public getAccountsResponse GetAccountsList()
{
AccountRepository objRepo=新AccountRepository();
GetAccountsResponse res=新的GetAccountsResponse();
List lstAccnts=新列表();
lstAccnts=objRepo.GetAccountsFromRepo();
foreach(lstAccnts中的var项目)
{
res.Accounts=新列表();
res.Accounts.ForEach(c=>c.AccountId=item.AccountId);
}
返回res;
}
请帮我解决这个问题。这很明显,因为您只创建了一个没有实例化任何对象的新列表 试试这样的
res.Accounts = new List<AccountsList>();
foreach (var item in lstAccnts)
{
res.Accounts.Add( new AccountsList(){AccountId =item.AccountId});
}
res.Accounts=新列表();
foreach(lstAccnts中的var项目)
{
Add(new AccountsList(){AccountId=item.AccountId});
}
这一点很明显,因为您只创建了一个新列表,根本没有实例化任何对象
试试这样的
res.Accounts = new List<AccountsList>();
foreach (var item in lstAccnts)
{
res.Accounts.Add( new AccountsList(){AccountId =item.AccountId});
}
res.Accounts=新列表();
foreach(lstAccnts中的var项目)
{
Add(new AccountsList(){AccountId=item.AccountId});
}
这一点很明显,因为您只创建了一个新列表,根本没有实例化任何对象
试试这样的
res.Accounts = new List<AccountsList>();
foreach (var item in lstAccnts)
{
res.Accounts.Add( new AccountsList(){AccountId =item.AccountId});
}
res.Accounts=新列表();
foreach(lstAccnts中的var项目)
{
Add(new AccountsList(){AccountId=item.AccountId});
}
这一点很明显,因为您只创建了一个新列表,根本没有实例化任何对象
试试这样的
res.Accounts = new List<AccountsList>();
foreach (var item in lstAccnts)
{
res.Accounts.Add( new AccountsList(){AccountId =item.AccountId});
}
res.Accounts=新列表();
foreach(lstAccnts中的var项目)
{
Add(new AccountsList(){AccountId=item.AccountId});
}
您在哪一行获得此消息看起来lstAccnts可能从调用objRepo.GetAccountsFromRepo()返回nullres.Accounts=new List();res.Accounts.ForEach(c=>c.AccountId=item.AccountId)代码>这两行没有意义。是否创建新列表
和ForEach
?它总是零elements@K.B我在res.Accounts.ForEach中得到错误(c=>c.AccountId=item.AccountId)@Baldrick否它正在返回您正在获取的哪一行中的帐户列表这看起来像是lstAccnts可能从调用返回nullobjRepo.GetAccountsFromRepo()
res.Accounts=new list();res.Accounts.ForEach(c=>c.AccountId=item.AccountId)代码>这两行没有意义。是否创建新列表
和ForEach
?它总是零elements@K.B我在res.Accounts.ForEach中得到错误(c=>c.AccountId=item.AccountId)@Baldrick否它正在返回您正在获取的哪一行中的帐户列表这看起来像是lstAccnts可能从调用返回nullobjRepo.GetAccountsFromRepo()
res.Accounts=new list();res.Accounts.ForEach(c=>c.AccountId=item.AccountId)代码>这两行没有意义。是否创建新列表
和ForEach
?它总是零elements@K.B我在res.Accounts.ForEach中得到错误(c=>c.AccountId=item.AccountId)@Baldrick否它正在返回您正在获取的哪一行中的帐户列表这看起来像是lstAccnts可能从调用返回nullobjRepo.GetAccountsFromRepo()
res.Accounts=new list();res.Accounts.ForEach(c=>c.AccountId=item.AccountId)代码>这两行没有意义。是否创建新列表
和ForEach
?它总是零elements@K.B我在res.Accounts.ForEach中得到错误(c=>c.AccountId=item.AccountId)@Baldrick不,它正在返回帐户列表。这也没有意义。编辑后立即查看原因。这将从lstaccts创建一个新列表,并将项目添加到res.accounts这也没有意义。这也没有意义。编辑后立即查看原因。这将从lstaccts创建一个新列表,并将项目添加到res.accounts这也是原因没有意义编辑后现在看一看为什么不这样做将从lstAccnts创建一个新列表并将项目添加到res.Accounts这也没有意义编辑后现在看一看为什么不这样做将从lstAccnts创建一个新列表并将项目添加到res.Accounts