How to get a single value using an entity structure

I have

     table product with colums product_id 
                               prodcut_name
                               category_id

                another table  category 
                               category_id
                               category_name

and I populate this product data using datagridview, which works fine

and I need to get the category name for the selected row in datagridview, because I did it 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

  error : cannot implicitly convert type sysytem.linq.iqueryble<string> to string ...

what do I need to do to get the name of one category for the selected cell in the productgam column productgridview

any suggestions .. pls ..

many thanks....

Changed code:              received 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.
+5
source share
2 answers

, . 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();
+7

  string categorynames = catogynames;

catogynames IQueryble. - ,

List<String> categorynames = catogynames.ToList();

catogynames.

P.S : D.

0

All Articles