我有一个
table product with colums product_id prodcut_name category_id another table category category_id category_name
我正在使用datagridview填充这些产品详细信息,工作正常
我需要获取datagridview中所选行的类别名称,因为我已经这样做….
private void productGridview_Cellclick(object sender,DataGridViewCellEventArgs e) { string productname = Convert.ToString(selectedRow.Cells["productnam"].Value); var categoryids = from cats in abc.products where cats.product_Name.Equals(productname) select cats.category_Id; var catogynames = from categorytypes in abc.categories where categorytypes.category_Name.Equals(categoryids) select categorytypes.category_Name; string categorynames = catogynames; }
得到了
error : cannot implicitly convert type sysytem.linq.iqueryble<string> to string ...
我需要做什么来获得productgridview productnam列中所选单元格的单个类别名称
任何建议..请..
非常感谢….
修改代码:
得到一个错误:
not supported exception: Unable to create a constant value of type 'System.Object'. Only primitive types ('such as Int32,String,and Guid') are supported in this context.
解决方法
您的查询返回的行为类似于集合的查询对象.您可以使用First,FirstOrDefault,Single或SingleOrDefault分别从查询中检索第一个或单个项目.
var categoryid = (from cats in abc.products where cats.product_Name.Equals(productname) select cats.category_Id).SingleOrDefault(); string categoryname = (from categorytypes in abc.categories where categorytypes.category_Name.Equals(categoryid) select categorytypes.category_Name).SingleOrDefault();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。