我想在WCF中返回对象列表,我的问题是如何在我的应用程序中实现它
public List GetConsumer(string transformer, string account) { var search = new List();
using (var conn = new MySqlConnection(ConnString))
{
var item = new SearchConsumer();
conn.Open();
const string cmdText = "SELECT * FROM tblconsumer_account WHERE transformer=@tsf AND accountNumber=@acct";
var cmd = new MySqlCommand(cmdText, conn);
cmd.Parameters.AddWithValue("@tsf", transformer);
cmd.Parameters.AddWithValue("@acct", account);
var reader = cmd.ExecuteReader();
while (reader.Read())
{
item.SearchResult = true;
item.AccountNumber = reader["accountNumber"].ToString();
item.TrasformerNumber = reader["transformer"].ToString();
item.Firstname = reader["firstname"].ToString();
item.LastName = reader["lastname"].ToString();
item.Address = reader["address"].ToString();
item.SerialNumber = reader["serialNumber"].ToString();
search.Add(item);
}
}
return search.ToList();
}
iam使用此guide作为参考,但我在应用程序中查找属性时遇到问题,例如e.Result.Lastname
,e.Result.Firstname
答案 0 :(得分:0)
更改以下内容
public List GetConsumer(string transformer, string account)
到
public List<SearchConsumer> GetConsumer(string transformer, string account) {
然后在您的移动项目上更新您的服务参考。
然后您就可以执行以下操作:
using(var service = new MyService()) //MyService is the name of your service reference
{
var searchConsumers = service.GetConsumer(transformer, account);
//Then loop your list
foreach(var searchConsumer in searchConsumers)
{
//Now access properties
//searchConsumer.Firstname, searchConsumer.Lastname
}
}
如果您只想返回单个对象,则需要将签名更改为以下内容:
public SearchConsumer GetConsumer(string transformer, string account)
并将函数的主体更改为仅获取一个项目,其用法为:
using(var service = new MyService()) //MyService is the name of your service reference
{
var searchConsumer = service.GetConsumer(transformer, account);
//Now access properties
//searchConsumer.Firstname, searchConsumer.Lastname
}
修改强>
如果您打算使用异步Web服务调用,则需要执行类似这样的操作
using(var service = new MyService()) //MyService is the name of your service reference
{
service.GetConsumerCompleted += GetConsumerCompletedCallback;
service.GetConsumerAsync(transformer, account);
}
//this method can be auto generated when hitting tab twice in visual studio after adding +=
public void (object sender, GetConsumerCompletedEventArgs e)
{
//e.Result.Firstname, e.Result.Lastname
//or if you are returning a list then loop over using code above
}