将来自 WCF 服务的通用列表转换为数据表时遇到问题
我对如何使用通用方法将通用列表解析为数据表/数据集感到困惑。我的设置: 1. 我在 WCF 服务库中定义了一个 Customers 类。
namespace Wcf.Sample.ServiceLibrary
{
public class Customers
{
public string ID = string.Empty;
public string CompanyName = string.Empty;
public string ContactName = string.Empty;
}
}
2. 我使用此类从我的OperationContract 返回通用列表。
namespace Wcf.Sample.ServiceLibrary
{
[ServiceContract]
public interface ICustomerService
{
[OperationContract]
List<Customers> GetAllCustomers();
}
}
3. 在Web客户端页面使用WCF服务。单击按钮时,我使用从 GetAllCustomers() 返回的列表填充 GridView。这工作得很好。
GridView1.DataSource = client.GetAllCustomers();
GridView1.DataBind();
4.现在的问题是,由于某种原因(排序/分页功能),我想将返回的通用列表实际转换为数据表。为此,我有一个方法返回一个我想要绑定到 GridView 的数据表。方法如下:
public static DataTable ConvertTo<T>(System.Collections.Generic.List<T> genericList)
{
//create DataTable Structure
DataTable dataTable = CreateTable<T>();
Type entType = typeof(T);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
//get the list item and add into the list
foreach (T item in genericList)
{
DataRow row = dataTable.NewRow();
foreach (PropertyDescriptor prop in properties)
{
row[prop.Name] = prop.GetValue(item);
}
dataTable.Rows.Add(row);
}
return dataTable;
}
public static DataTable CreateTable<T>()
{
//T –> ClassName
Type entType = typeof(T);
//set the datatable name as class name
DataTable dataTable = new DataTable(entType.Name);
//get the property list
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
foreach (PropertyDescriptor prop in properties)
{
//add property as column
dataTable.Columns.Add(prop.Name, prop.PropertyType);
}
return dataTable;
}
我不知道如何调用这个函数?如何指定实际上位于 Web 服务中的 as Customers 类?彻底迷失了。如果有人可以指导我使用以下代码以及如何使其工作,我将不胜感激。
GridView1.DataSource = ConvertTo<???>(client.GetAllCustomers());
I am confused on how can I use generic methods to parse generic list into datatable/dataset. My setup:
1. I have a class Customers defined in WCF Service Library.
namespace Wcf.Sample.ServiceLibrary
{
public class Customers
{
public string ID = string.Empty;
public string CompanyName = string.Empty;
public string ContactName = string.Empty;
}
}
2. I use this class to return a generic list from my OperationContract.
namespace Wcf.Sample.ServiceLibrary
{
[ServiceContract]
public interface ICustomerService
{
[OperationContract]
List<Customers> GetAllCustomers();
}
}
3. Consume WCF Service in web client page. On button click I populate the GridView with the list returned from GetAllCustomers(). This works perfectly fine.
GridView1.DataSource = client.GetAllCustomers();
GridView1.DataBind();
4. Now the issue is, for some reason (sort/paging function) I want to actually convert the returned generic list into a datatable. To do so, I have a method that returns me a datatable which I want to bind to a GridView. Here are the methods:
public static DataTable ConvertTo<T>(System.Collections.Generic.List<T> genericList)
{
//create DataTable Structure
DataTable dataTable = CreateTable<T>();
Type entType = typeof(T);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
//get the list item and add into the list
foreach (T item in genericList)
{
DataRow row = dataTable.NewRow();
foreach (PropertyDescriptor prop in properties)
{
row[prop.Name] = prop.GetValue(item);
}
dataTable.Rows.Add(row);
}
return dataTable;
}
public static DataTable CreateTable<T>()
{
//T –> ClassName
Type entType = typeof(T);
//set the datatable name as class name
DataTable dataTable = new DataTable(entType.Name);
//get the property list
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entType);
foreach (PropertyDescriptor prop in properties)
{
//add property as column
dataTable.Columns.Add(prop.Name, prop.PropertyType);
}
return dataTable;
}
I am not sure how to call this function? How can I specify the as Customers class which is actually in a webservice? Totally lost. I would appreciate if someone can guide me on the following code, how to make it work.
GridView1.DataSource = ConvertTo<???>(client.GetAllCustomers());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够通过修改 WCF 服务本身来解决这个问题(尽管我不愿意这样做)。我修改了 GetAllCustomers 方法以返回数据表而不是泛型类型。在服务本身中,我使用相同的方法将泛型类型转换为数据表:
我注意到的另一件事是,以下行
始终为我的类型返回 null。这是因为我在 Customers 类中没有任何 get/set 方法。我在 Customer 类中创建了 get/set 方法,一切都很顺利。
感谢所有提供帮助和试图提供帮助的人:)
I was able to resolve this issue by modifing the WCF Service itself (although I was reluctant to do so). I modified the GetAllCustomers method to return a datatable instead of generic type. In the service itself, I am converting the generic type into datatable using the same methods:
Another thing that I noticed is that the following line
would always returned null for my type. This was due to the fact that I didn't have any get/set methods in Customers class. I created get/set methods in Customer class and everything worked like a charm.
Thanks to everyone who helped and those who tried to help :)