views:

116

answers:

1

Hey, I am very new to entity, sql, c#, and asp.net so this might be something easily fixed.

I am attempting to display all the inactive products stored in my table called products in a datagrid.

var productQuery = from b in solutionContext.Version
                   where b.Product.Name == search && b.Product.ActiveNumber > b.VersionNumber
                   select new Product
                   {
                                   Name = b.Product.Name,
                                   Description = b.Product.Description,
                                   ID = b.ID,
                                   LastNumber = b.Product.LastNumber,
                                   MiddleNumber = b.Product.MiddleNumber,
                                   RSTATE = b.RSTATE,
                                   ActiveNumber = b.Product.ActiveNumber,
                                   LastModified = b.Product.LastModified,
                                   ParentID = b.Product.ParentID,
                                   ProductType = b.Product.ProductType
                  };

                  ProductsGrid.DataSource = productQuery;
                  ProductsGrid.DataBind();

I am getting this error:

  • $exception {"The entity or complex type 'SolutionsModel.Product' cannot be constructed in a LINQ to Entities query."} System.Exception {System.NotSupportedException}

Thanks for any advice!

+3  A: 

The error is saying that you cannot generate a LINQ to Entities object of type Product. But you can return it. Can you try to return b.Product that might work correctly?

var productQuery = from b in solutionContext.Version
                   where b.Product.Name == search && b.Product.ActiveNumber > b.VersionNumber
                   select b.Product; 

                  ProductsGrid.DataSource = productQuery;
                  ProductsGrid.DataBind();
azamsharp
thanks, but where should I place the return? Sorry, I am still trying to figure much of this out.
PFranchise
Thank you very much kind sir. I'm sure I will have more questions during the life of this project, but this is a very helpful step. Have a great day
PFranchise
O, if you don'd mind I have one side question. Each inactive product has a different version, which have different RSTATEs. Those are stored in my Versions table. Since I return Product objects, I need to alter the RSTATE of those to be the same as the Version objects that they are related to. There is a one-to-many relationship going from product to the versions. How can I access those and change those attributes on the products i returned. I assume it will be something like
PFranchise
foreach (var product in productQuery) { product.RSTATE = product.Versions.RSTATE; }But that obviously is not it I guess I don't know how to go backwards on this one to many relationship once the query is completed as you showed me.
PFranchise