Linq: the linked objects are null, why?

Posted by user46503 on Stack Overflow See other posts from Stack Overflow or by user46503
Published on 2010-04-10T02:10:09Z Indexed on 2010/04/10 2:13 UTC
Read the original article Hit count: 393

Filed under:
|

Hello, I have several linked tables (entities). I'm trying to get the entities using the following linq:

ObjectQuery<Location> locations = context.Location;
ObjectQuery<ProductPrice> productPrice = context.ProductPrice;
ObjectQuery<Product> products = context.Product;
IQueryable<ProductPrice> res1 = from pp in productPrice
                    join loc in locations
                    on pp.Location equals loc
                    join prod in products
                    on pp.Product equals prod
                    where prod.Title.ToLower().IndexOf(Word.ToLower()) > -1
                    select pp;

This query returns 2 records, ProductPrice objects that have linked object Location and Product but they are null and I cannot understand why. If I try to fill them in the linq as below:

res =
                from pp in productPrice
                join loc in locations
                on pp.Location equals loc
                join prod in products
                on pp.Product equals prod
                where prod.Title.ToLower().IndexOf(Word.ToLower()) > -1
                select new ProductPrice
                {
                    ProductPriceId = pp.ProductPriceId,
                    Product = prod
                };

I have the exception "The entity or complex type 'PBExplorerData.ProductPrice' cannot be constructed in a LINQ to Entities query" Could someone please explain me what happens and what I need to do? Thanks

© Stack Overflow or respective owner

Related posts about ADO.NET

Related posts about linq-to-entities