无法转换为“System.Collections.IEnumerable”
本文关键字:Collections IEnumerable System 转换 | 更新日期: 2023-09-27 18:34:05
我在类 ProductSearch.cs
中有以下方法,但我在第 PagedCollectionView pagingCollection = new PagedCollectionView(e.Result)
行收到错误
无法从"产品搜索.产品列表"转换为 'System.Collections.IEnumerable'
void service_GetObjectCompleted(object sender, GetObjectCompletedEventArgs e)
{
if (e.Result.Count != 0)
{
PagedCollectionView pagingCollection = new PagedCollectionView(e.Result);
pgrProductGrids.Source = pagingCollection;
grdProductGrid.ItemsSource = pagingCollection;
}
}
e
包含一个类 ListOfProducts
的对象,该对象从类中存在的此方法获取其值service.svn.cs
public ListOfProducts GetObject()
{
ListOfProducts Listproducts = new ListOfProducts();
........
using (IDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Product product = new Product(reader["Name"].ToString(), reader["Code"].ToString());
Listproducts.Product.Add(product);
}
}
return Listproducts;
}
这是ListOfProducts
类
public class ListOfProducts
{
[DataMember()]
public List<Product> Product { get; set; }
public ListOfProducts()
{
Product = new List<Product>();
}
}
尝试下面的代码。应该适合您的情况
PagedCollectionView pagingCollection = new PagedCollectionView(e.Result.Product);
和整个代码
void service_GetObjectCompleted(object sender, GetObjectCompletedEventArgs e)
{
if (e.Result.Count != 0)
{
PagedCollectionView pagingCollection = new PagedCollectionView(e.Result.Product);
pgrProductGrids.Source = pagingCollection;
grdProductGrid.ItemsSource = pagingCollection;
}
}
您也可以使用indexer
public Product this[int index]
{
get { return Product[index]; }
set { Product[index] = value; }
}