C# 得到一个错误;不支持用户代码未处理异常;

C# 得到一个错误;不支持用户代码未处理异常;,c#,winforms,combobox,notsupportedexception,C#,Winforms,Combobox,Notsupportedexception,我有一张桌子 product(table name) product_id product_name product_image product_price product_description category_id category(table name ) category_id category_name category_des

我有一张桌子

  product(table name)
        product_id
        product_name 
        product_image
        product_price
        product_description
        category_id

  category(table name )
        category_id
        category_name 
        category_description
我有一个组合框,名为
categoryCombobox
,网格视图名为
productgridview

我试图根据组合框中的选择填充数据网格。。像这样

       private viod form_load(object sender, EventArgs e)
       {
        var products = from prods in abc.products
                       select new
                       {
                           prods.product_Id,
                           productname =  prods.product_Name,
                           productimage = prods.product_Image,
                           productprice = prods.product_Price,
                           productdescription = prods.product_Description

                       };
        productbindingsource.DataSource = products;
        productgridview.DataSource = productbindingsource;
        productgridview.Columns[0].Visible = false;
       }
       private void categoryCombobox_SelectedIndexChanged(object sender, EventArgs e)
       {

          // is this query correct 
          var categoryid = from productcategories in abc.categories
                         where productcategories.category_Name.Equals(categoryCombobox.Text)
                         select productcategories.category_Id;

          var produc = from pros in abc.products
                       where pros.Category_Id.Equals(categoryid)
                       select new
                       {
                           productname = pros.product_Name,
                           productimage = pros.product_Image,
                           productprice = pros.product_Price,
                           productdescription = pros.product_Description                                   

                       };
        productbindingsource.DataSource = produc;
        productgridview.DataSource = productbindingsource;
        productgridview.Columns[0].Visible = false;

    }      
有这样一个错误

错误:在此行productbindingsource.DataSource=produc

不支持异常已由用户代码解除绑定
无法比较“System.Linq.IQueryable”“1”类型的元素。 只有基元类型(如Int32、字符串和Guid)和实体 类型是受支持的


您正在尝试将int字段与可枚举集进行比较

如果categoryID查询只返回一个值,请尝试以下操作:

var produc = from pros in abc.products
                   where pros.Category_Id.Equals(categoryid.Single())
                   select new
                   {
                       productname = pros.product_Name,
                       productimage = pros.product_Image,
                       productprice = pros.product_Price,
                       productdescription = pros.product_Description                                   

                   };
如果它应该返回一个ID列表,那么您需要编写一个带有连接的查询。根据名称
categoryId

编辑-可能不是100%语法正确

var produc = from pros in abc.products
    join cats in abc.categories on cats.category_id equals pros.Category_Id
    where cats.category_Name.Equals(categoryCombobox.Text)
    select new
    {
        productname = pros.product_Name,
        productimage = pros.product_Image,
        productprice = pros.product_Price,
        productdescription = pros.product_Description                                   
    };
调试时将鼠标悬停在
var
上。您将看到它不是您期望的id,而是一个
IEnumerable
。你想做的是

// .First() trips the query and returns a single category_Id
var id = (from productcategories in abc.categories
         where productcategories.
                   category_Name.Equals(categoryCombobox.Text)
         select productcategories.category_Id).First();

var produc = from pros in abc.products
               where pros.Category_Id.Equals(id)
               select new
               {
                   productname = pros.product_Name,
                   productimage = pros.product_Image,
                   productprice = pros.product_Price,
                   productdescription = pros.product_Description                                   
               };

请注意
ids.First()
,它从初始查询中获取第一个结果。

需要更多信息才能提供帮助,例如abc.products的组成。您不了解错误的哪一部分?这很好地解释了这个问题clearly@kieren如果你明白了,请尝试解决这个问题。我还是遇到了同样的问题。你能修改一下你的格式吗?我会再说一遍“你不明白错误的哪一部分?”。。它表示不能比较类型为
IQueryable
的元素,必须比较基元类型。所以,问题是,您试图比较
IQueryable
对象。不,该查询返回IQueryable。我使用linq查询进行编辑,该查询在内部进行连接,而不是两次数据库往返。这就是我在使用您的查询错误时遇到的问题:方法“First”只能用作最终查询操作。考虑在这个实例中使用方法“FrestRealDebug”。非常感谢你的支持。。
// .First() trips the query and returns a single category_Id
var id = (from productcategories in abc.categories
         where productcategories.
                   category_Name.Equals(categoryCombobox.Text)
         select productcategories.category_Id).First();

var produc = from pros in abc.products
               where pros.Category_Id.Equals(id)
               select new
               {
                   productname = pros.product_Name,
                   productimage = pros.product_Image,
                   productprice = pros.product_Price,
                   productdescription = pros.product_Description                                   
               };