另一个不能隐式转换IEnumerable类型

时间:2014-05-11 03:25:39

标签: c# linq

我很抱歉发布了另一个转换类型错误,但在查看并尝试了近两天的每个示例之后,我无法让它工作。

我需要做些什么才能让它发挥作用????

    public IEnumerable<Product> BindProductDropDown([Control("ddBrand")] int BrandID)
    {
        if (BrandID > 0)
        {
            try
            {


                var prod = (from p in _DbContext.Products
                            join pc in _DbContext.ProductCategories on p.ProductCategoryID equals pc.ProductCategoryId
                            where pc.BrandID == BrandID
                            orderby p.OrderBy, p.Name
                            select new 
                            {
                                p.ProductID,
                                p.ItemNumber
                            });
                var c = prod.Count();
                return prod;

            }
            catch (Exception exp)
            {
                ErrorLabel.Text = exp.Message;
                return null;
            }
        }
        else
        {
            ErrorLabel.Text = "Please select a brand from the drop drown list of brands above.";
            return null;
        }
    }
  public IEnumerable<Product> BindProductDropDown([Control("ddBrand")] int BrandID)
    {
        if (BrandID > 0)
        {
            try
            {


                var prod = (from p in _DbContext.Products
                            join pc in _DbContext.ProductCategories on p.ProductCategoryID equals pc.ProductCategoryId
                            where pc.BrandID == BrandID
                            orderby p.OrderBy, p.Name
                            select new Product
                            {
                                p.ProductID,
                                p.ItemNumber
                            });
                var c = prod.Count();
                return prod;

            }
            catch (Exception exp)
            {
                ErrorLabel.Text = exp.Message;
                return null;
            }
        }
        else
        {
            ErrorLabel.Text = "Please select a brand from the drop drown list of brands above.";
            return null;
        }
    }

1 个答案:

答案 0 :(得分:0)

prodIEnumerable<AnonymousType>,您需要创建Product才能进行编译。

select new Product
    {
       ProductID = p.ProductID,
       ItemNumber = p.ItemNumber
    });

如果您不了解&#34;匿名类型&#34;你可以阅读herehere