我正在循環訪問我的XML文件以捕獲存款清單並將它們作爲列表傳遞給我的業務邏輯層。我在退貨聲明中收到一條錯誤消息,說匿名類型集合不包含to.list
的定義。如果我將to.list
從退貨聲明中刪除,我在select
上發現錯誤,說我錯過了演員陣容,因爲它無法將匿名收藏轉換爲列表。我該如何解決這個問題?在WinForms中投射集合
數據訪問層
public class DepositList
{
public string Depid { get; set; }
public string Amount { get; set; }
public string DepDate { get; set; }
}
public class DLDeposits
{
public List<DepositList> getDeposits(string customerid)
{
double sumDep = 0;
//Returns list of deposits for selected customer
var doc = XDocument.Load("Portfolio.xml");
List<DepositList> result = from account in doc.Descendants("account")
from deposit in account.Elements("deposits")
where (string)account.Element("acct").Attribute("custid").Value == customerid
select new
{
Depid = (string)deposit.Attribute("depid").Value,
Amount = (string)deposit.Attribute("depamount").Value,
DepDate = (string)deposit.Attribute("depdate").Value
}.ToList();
return result;
}
}
業務邏輯層
public double getDeposits(string customerId)
{
double sumDep = 0;
//Returns list of deposits for selected customer
var doc = XDocument.Load("Portfolio.xml");
CustCount(doc);
DLDeposits obj = new DLDeposits();
var depositList = obj.getDeposits(customerId);
for (int i = 0; i < NumCusts; i++)
{
BL_Deposit oDeposit = new BL_Deposit();
oDeposit.DepAmt = Convert.ToDouble(depositList[i].Amount);
oDeposit.DepDate = Convert.ToDateTime(depositList[i].DepDate);
oDeposit.DepositId = Convert.ToInt32(depositList[i].Depid);
addDeposits(oDeposit);
sumDep += oDeposit.DepAmt;
}
return sumDep;
}