gpt4 book ai didi

c# - 查询的结果类型既不是 EntityType 也不是具有实体元素类型的 CollectionType

转载 作者:行者123 更新时间:2023-11-30 12:23:58 25 4
gpt4 key购买 nike

var myQuery = from product in _repository.Query()
join prodLocalization in _repoProductLocalization.Query()
on product.Id equals prodLocalization.ProductId
select new { Product = product, Localization = prodLocalization };
myQuery = myQuery.Include(x => x.Product.Customer);
var prods = myQuery.ToList();

最后一行抛出:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: The result type of the query is neither an EntityType nor a CollectionType with an entity element type. An Include path can only be specified for a query with one of these result types.

对于为什么会发生这种情况,我几乎找不到任何解释。有帮助吗?

最佳答案

您的类在ProductLocalization 之间有物理关系吗?如果他们这样做,您不应该使用 join。此外,您必须在选择之前调用 include。

试试这个:

var myQuery = from product in _repository.Query()
.Include(x => x.Product.Customer)
.Include(x => x.Product.Localization)
select new
{
Product = product,
Localization = product.Localization
};

var prods = myQuery.ToList();

关于c# - 查询的结果类型既不是 EntityType 也不是具有实体元素类型的 CollectionType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35202459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com