无法转换为' System.Collections.IEnumerable

时间:2016-02-10 12:18:51

标签: c# list collections

我在课程ProductSearch.cs中有以下方法,但我在第PagedCollectionView pagingCollection = new PagedCollectionView(e.Result)行收到错误

  

无法转换为' ProductSearch.ListOfProducts'至   ' 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的Object,它从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>();
    }
}

1 个答案:

答案 0 :(得分:0)

请尝试以下代码。应该适合您的情况

    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; }
}