I have product table with columns
product_id (p.k)
product_name
product_description
product_price
category_id
and I have set the properties for product_id
like this...(primary key , not null,A.I)
I am trying to represent the number of same products in datagrid view as a column like this
product_name product_description stock available product price
a good product 2 (a+a) 300
b bad product 3 (b+b+b) 400
by the following method....
var stockavailable = dbcontext.products
.GroupBy(x => x.product_Id)
.Select(a => new
{
productid = a.Key,
productnam = a.FirstOrDefault().product_Name,
productdescr = a.FirstOrDefault().product_Description,
stockavailable = a.LongCount(),
productprice = a.FirstOrDefault().product_Price
});
bindingsource.DataSource = stockavailable;
datagridview1.DataSource = bindingsource;
But it does not show the number of products even if there are two same products. with different product_id 's would any one pls help on this...
EDIT :
Can i do like this.....
stockavailable = a.select(x=>x.product_id).Distinct().Count()
Instead of this stockavailable = a.LongCount(),