C# C.Net WinForms应用程序:匿名收集-如何分类返回类型
对我来说,这似乎是一个再出错的问题。当我使用匿名集合时,我再次遇到类型转换问题。我对XML文件的查询返回字符串值的集合。我试图将这些值从数据访问层返回到业务逻辑层。提前谢谢C# C.Net WinForms应用程序:匿名收集-如何分类返回类型,c#,.net,winforms,casting,anonymous-types,C#,.net,Winforms,Casting,Anonymous Types,对我来说,这似乎是一个再出错的问题。当我使用匿名集合时,我再次遇到类型转换问题。我对XML文件的查询返回字符串值的集合。我试图将这些值从数据访问层返回到业务逻辑层。提前谢谢 public string[] getCustDetails(string customerId) { //Pulls customer information for selected customer var doc = XDocument.Load("Portfolio.
public string[] getCustDetails(string customerId)
{
//Pulls customer information for selected customer
var doc = XDocument.Load("Portfolio.xml");
var custRecord = from account in doc.Descendants("acct")
let acct = account.Element("acct")
where (string)account.Attribute("custid").Value == customerId
select new
{
Fname = (string)account.Attribute("fname").Value,
Lname = (string)account.Attribute("lname").Value,
Ssn = (string)account.Attribute("ssn").Value,
Dob = (string)account.Attribute("dob").Value,
Custid = (string)account.Attribute("custid").Value
};
return ?????
}
不能使用匿名类型作为方法的参数或返回类型 我建议创建一个具有所需属性的简单类,并使用它而不是匿名类型
public class AccountDetails
{
public string FName { get; set; }
public string LName { get; set; }
public string Ssn { get; set; }
public string Dob { get; set; }
public string Custid { get; set; }
}
public IEnumerable<AccountDetails> getCustDetails(string customerId)
{
//Pulls customer information for selected customer
var doc = XDocument.Load("Portfolio.xml");
var custRecords = from account in doc.Descendants("acct")
let acct = account.Element("acct")
where (string)account.Attribute("custid").Value == customerId
select new AccountDetails
{
Fname = (string)account.Attribute("fname").Value,
Lname = (string)account.Attribute("lname").Value,
Ssn = (string)account.Attribute("ssn").Value,
Dob = (string)account.Attribute("dob").Value,
Custid = (string)account.Attribute("custid").Value
};
return custRecords;
}
为需求创建新类并将其作为列表返回
public MyClass[] getCustDetails(string customerId)
{
//Pulls customer information for selected customer
var doc = XDocument.Load("Portfolio.xml");
var custRecord = (from account in doc.Descendants("acct")
let acct = account.Element("acct")
where (string)account.Attribute("custid").Value == customerId
select new MyClass
{
Fname = (string)account.Attribute("fname").Value,
Lname = (string)account.Attribute("lname").Value,
Ssn = (string)account.Attribute("ssn").Value,
Dob = (string)account.Attribute("dob").Value,
Custid = (string)account.Attribute("custid").Value
}).ToArray();
return custRecord;
}
将您的类定义为
public class MyClass
{
public string Fname { get; set; }
public string Lname { get; set; }
public string Ssn { get; set; }
public string Dob { get; set; }
public string Custid { get; set; }
}
我将创建一个包含以下属性的新类
string Fname
string Lname
string Ssn
string Dob
string Custid
然后从getCustDetails方法返回此类型此返回数组的外观如何?我不清楚您打算如何将匿名复合对象表示为字符串数组。如何键入返回到业务逻辑层的集合DLGetCustomers obj=new DLGetCustomers;??某些集合类型应该是相同的:obj.getcustdailssorry,我真的很难处理这些通用集合。@Susan-鉴于我不知道您期望得到的回报是什么类型,我实在帮不了您。您可以使用建议的AccountDetails类的列表。