Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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                                   
    };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top