English 中文(简体)
Linq:相关物体无效,原因何在?
原标题:Linq: the linked objects are null, why?

我有几个相关的表格(实体)。 I m试图利用下列支线使各实体受益:

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;

查询记录2 产品目标与产品挂钩,但无效,我无法理解原因。 如果我试图在小组中填满如下:

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

最佳回答

回答你的第一个问题,即产品与地点是无效的,因为你需要在你的询问中添加一个“包含”的内容。

  IQueryable<ProductPrice> res1 = from pp in 
                productPrice.Include("Location").Include("Product")
                  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;

第二个问题是欧洲法院试图推翻你的询问和产品价格(不是实体),以便它无法做到。 如果你想把它变成匿名的一类,那么这样做就是

            select new 
            {
                ProductPriceId = pp.ProductPriceId,
                Product = prod
            };

之后

          res.ToList().ConvertAll(x=new ProductPrice () {
               ProductPriceId  = x.ProductPriceId ,
               Product  = x.Product

           });

或者,你可以采取其他方式,选择你想要的实体,并仅用人手。

问题回答

暂无回答




相关问题
ADO.NET Entity Data Model are not precise enough

I run this code: var cos = from k in _db.klienci_do_trasy where k.klient_id == 5 select k; but the query send to database is: SELECT * FROM `klienci_do_trasy` LIMIT 0, 30 why is it for, there ...

Synchronising SQL database through ADO.Net

The problem that i m having is how can i synchronise my datasets in my VS 2008 project to any changes in the database. As you know we read data from the db into the dataset which is disconnected, now ...

ADO.NET Data Services - Uploading files

I am trying to write REST web service through which our clients can upload a file on our file server. IS there an example or any useful links which I can refer for any guidance? I haven t seen many ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

热门标签