Linq:链接的对象为空,为什么?

发布于 2024-08-28 12:19:29 字数 1269 浏览 6 评论 0原文

我有几个链接的表(实体)。我正在尝试使用以下 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;

此查询返回 2 条记录,即具有链接对象位置和产品的 ProductPrice 对象,但它们为空,我无法理解为什么。如果我尝试将它们填充到 linq 中,如下所示:

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
                };

我遇到异常“无法在 LINQ to Entities 查询中构造实体或复杂类型 'PBExplorerData.ProductPrice'” 有人可以向我解释一下发生了什么以及我需要做什么吗? 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我闹别瞎闹 2024-09-04 12:19:29

第一个问题“产品”和“位置”的答案为空,因为您需要在查询中添加 Include("") 。

  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;

第二个问题是 EF 试图压低您的查询和 ProductPrice (不是实体),所以它不能。如果您想将其转换为匿名类型,只需执行然后

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

执行

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

           });

或者您可以通过其他方式执行此操作,通过选择所需的实体,然后填充手册。

The answer to your first question the Product and Location are null because you need to add an Include("") to your query.

  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;

The second issue is EF is trying to push down your query and ProductPrice (is not an entity) so it can not. If you want to do this convert it to an anonymous type so just do

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

And then do

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

           });

Or you could do it other ways, by selecting the entity you want, and just populating manual.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文