Question

I'm sorry for posting another convert type error, but after looking through and trying every example for nearly two days now, I can't get this to work.

What do I need to do to get this to work????

    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;
        }
    }
Était-ce utile?

La solution

prod is a IEnumerable<AnonymousType> you need to create Product in order to compile.

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

If you're not aware of "Anonymous Types" you can read about it here and here

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top