I have a
table product with colums product_id
prodcut_name
category_id
another table category
category_id
category_name
and i am populating these product details using datagridview that working fine
and i need to get the categoryname for selected row in datagridview for that i have done like this....
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;
}
got an
error : cannot implicitly convert type sysytem.linq.iqueryble<string> to string ...
what i need to do to get the single category name for selected cell in productgridview productnam column
any suggestions.. pls ..
many thanks....
Modified Code: got an error :
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.
Your query is returning a query object that behaves like a collection. You can use First
, FirstOrDefault
, Single
, or SingleOrDefault
to retrieve the first or single item from the query respectively.
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();
your problem with casting is here
string categorynames = catogynames;
catogynames
is IQueryble
. You can do something like this,
List<String> categorynames = catogynames.ToList();
Or you can iterate through catogynames
.
PS please review your variable names :D.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.