C# 在WinForms中转换集合
我在XML文件中循环,以捕获存款列表,并将它们作为列表传递给我的业务逻辑层。我在return语句中得到一个错误,该语句表示匿名类型集合不包含C# 在WinForms中转换集合,c#,.net,collections,C#,.net,Collections,我在XML文件中循环,以捕获存款列表,并将它们作为列表传递给我的业务逻辑层。我在return语句中得到一个错误,该语句表示匿名类型集合不包含to.list的定义。如果我将to.list从return语句中删除,我会在select上得到一个错误,说我缺少一个cast,因为它无法将匿名集合转换为列表。我如何解决这个问题 数据访问层 public class DepositList { public string Depid { get; set; } public string Am
to.list
的定义。如果我将to.list
从return语句中删除,我会在select
上得到一个错误,说我缺少一个cast,因为它无法将匿名集合转换为列表。我如何解决这个问题
数据访问层
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;
}
公共类存款清单
{
公共字符串Depid{get;set;}
公共字符串金额{get;set;}
公共字符串DepDate{get;set;}
}
公营存款
{
公共列表getDeposits(字符串customerid)
{
双sumDep=0;
//返回所选客户的存款列表
var doc=XDocument.Load(“Portfolio.xml”);
列表结果=来自单据子体中的科目(“科目”)
来自存款账户要素(“存款”)
其中(字符串)account.Element(“acct”).Attribute(“custid”).Value==customerid
选择新的
{
Depid=(字符串)deposit.Attribute(“Depid”).Value,
金额=(字符串)存款.Attribute(“depamount”).Value,
DepDate=(字符串)存款.Attribute(“DepDate”).Value
}.ToList();
返回结果;
}
}
业务逻辑层
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;
}
public-double-getDeposits(字符串customerId)
{
双sumDep=0;
//返回所选客户的存款列表
var doc=XDocument.Load(“Portfolio.xml”);
客户计数(doc);
DLDeposits obj=新的DLDeposits();
var存款清单=对象获取存款(客户ID);
对于(int i=0;i
问题在于您正在创建一个匿名类型的新列表,而不是列表。您只需将select
子句更改为:
select new DepositList
{
Depid = (string) deposit.Attribute("depid"),
Amount = (string) deposit.Attribute("depamount"),
DepDate = (string) deposit.Attribute("depdate")
}
请注意,我已经删除了Value
属性的使用-您不需要同时使用该属性和转换为字符串,并且通过使用从XAttribute
到string
的显式转换,如果缺少属性,您将得到null
而不是NullReferenceException
然而,我突然想到,如果存款清单
的类型更强一些会更好,比如:
public class DepositList
{
public int Depid { get; set; }
public decimal Amount { get; set; }
public DateTime DepDate { get; set; }
}
然后你可以使用:
select new DepositList
{
Depid = (int) deposit.Attribute("depid"),
Amount = (decimal) deposit.Attribute("depamount"),
DepDate = (DateTime) deposit.Attribute("depdate")
}
LINQ到XML将为您进行转换。(在本例中,如果缺少任何属性,它将抛出异常,因为我使用的是不可为null的值类型。)
请注意,我将Amount
改为decimal
,而不是double
。您不应该对财务值使用double
。问题是您正在创建一个匿名类型的新列表,而不是列表。您只需将select
子句更改为:
select new DepositList
{
Depid = (string) deposit.Attribute("depid"),
Amount = (string) deposit.Attribute("depamount"),
DepDate = (string) deposit.Attribute("depdate")
}
请注意,我已经删除了Value
属性的使用-您不需要同时使用该属性和转换为字符串,并且通过使用从XAttribute
到string
的显式转换,如果缺少属性,您将得到null
而不是NullReferenceException
然而,我突然想到,如果存款清单
的类型更强一些会更好,比如:
public class DepositList
{
public int Depid { get; set; }
public decimal Amount { get; set; }
public DateTime DepDate { get; set; }
}
然后你可以使用:
select new DepositList
{
Depid = (int) deposit.Attribute("depid"),
Amount = (decimal) deposit.Attribute("depamount"),
DepDate = (DateTime) deposit.Attribute("depdate")
}
LINQ到XML将为您进行转换。(在本例中,如果缺少任何属性,它将抛出异常,因为我使用的是不可为null的值类型。)
请注意,我将Amount
改为decimal
,而不是double
。您不应该对财务价值使用double