【问题标题】:Got an error "Not supportedException was unhandled by user code"收到错误“用户代码未处理 Not supportedException”
【发布时间】:2011-08-17 12:17:05
【问题描述】:

我有桌子

  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、String 和 Guid)和实体 支持类型。

【问题讨论】:

  • 需要更多信息才能提供帮助,例如 abc.products 的构成。
  • 错误的哪一部分你不明白?它非常清楚地解释了问题
  • @kieren 如果你理解请尝试解决这个问题我仍然遇到同样的问题
  • 您能修改一下格式吗?
  • 我再说一遍“错误的哪一部分你不明白?”.. 它说你不能比较 IQueryable 类型的元素,你必须比较原始类型。所以,问题是,您正在尝试比较 IQueryable 对象..

标签: c# winforms combobox notsupportedexception


【解决方案1】:
var categoryid = from productcategories in abc.categories
                 where productcategories.
                           category_Name.Equals(categoryCombobox.Text)
                 select productcategories.category_Id;

在调试时将鼠标悬停在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(),它从初始查询中获取第一个结果。

【讨论】:

  • 这就是我在使用您的查询时遇到的问题错误:方法“First”只能用作最终查询操作。请考虑在此实例中使用“FirstOrDefault”方法。
  • 其中 pros.Category_Id.Equals(categoryid.FirstOrDefault()) 我已经使用了这个查询,它现在正在工作..非常感谢您的支持..
【解决方案2】:

您尝试将 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                                   
    };

【讨论】:

  • 否,该查询返回 IQueryable。我使用 linq 查询进行了编辑,该查询在内部进行连接,而不是两次数据库往返。
猜你喜欢
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-28
  • 2014-10-18
相关资源
最近更新 更多