Linq:链接的对象为空,为什么?
我有几个链接的表(实体)。我正在尝试使用以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个问题“产品”和“位置”的答案为空,因为您需要在查询中添加 Include("") 。
第二个问题是 EF 试图压低您的查询和 ProductPrice (不是实体),所以它不能。如果您想将其转换为匿名类型,只需执行然后
执行
或者您可以通过其他方式执行此操作,通过选择所需的实体,然后填充手册。
The answer to your first question the Product and Location are null because you need to add an Include("") to your query.
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
And then do
Or you could do it other ways, by selecting the entity you want, and just populating manual.