出现错误“用户代码未处理 NotsupportedException”
我有一个表,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调试时将鼠标悬停在
var
上。您将看到它不是您期望的 id,而是一个IEnumerable
。您要做的是注意
ids.First()
,它从初始查询中获取第一个结果。Hover over
var
while debugging. You will see that it is NOT an id as you expect, but anIEnumerable
. What you want to do isNotice
ids.First()
, which takes the first result from the initial query.您试图将 int 字段与可枚举集进行比较。
如果categoryID 查询仅返回单个值,请尝试以下操作:
如果它应该返回 Id 列表,则您将需要编写带有联接的单个查询。我认为根据名称
categoryId
编辑它应该是单一的 - 可能不是 100% 语法正确
Your trying to compare an int field against an enumerable set.
If the categoryID query will only return a single value, try this:
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