出现错误“用户代码未处理 NotsupportedException”

发布于 2024-11-29 22:21:24 字数 2369 浏览 5 评论 0原文

我有一个表,

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

I have table

  product(table name)
        product_id
        product_name 
        product_image
        product_price
        product_description
        category_id

  category(table name )
        category_id
        category_name 
        category_description

I have a combobox named it as categoryCombobox and grid view named it as productgridview

am trying to populate the datagrid depending upon the selection in the combobox.. like this ....

       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;

    }      

Got an error like this ......

ERROR : At this line productbindingsource.DataSource = produc;

Not supportedException was unhaldled by user code
Cannot compare elements of type 'System.Linq.IQueryable`1'.
Only primitive types (such as Int32, String, and Guid) and entity
types are supported.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉生梦死 2024-12-06 22:21:24
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(),它从初始查询中获取第一个结果。

var categoryid = from productcategories in abc.categories
                 where productcategories.
                           category_Name.Equals(categoryCombobox.Text)
                 select productcategories.category_Id;

Hover over var while debugging. You will see that it is NOT an id as you expect, but an IEnumerable. What you want to do is

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

Notice ids.First(), which takes the first result from the initial query.

暗藏城府 2024-12-06 22:21:24

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

Your trying to compare an int field against an enumerable set.

If the categoryID query will only return a single value, try this:

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                                   

                   };

If it's supposed to return a list of Ids, you'll want to write a single query with a join. I assume it's supposed to be single based on the name categoryId

edit - might not be 100% syntax correct

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                                   
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文