The query result type is neither EntityType nor CollectionType with entity element type

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();

Last line:

An exception of type "System.InvalidOperationException" occurred in EntityFramework.SqlServer.dll, but was not processed in the user code

Additional Information: The query result type is not EntityType or CollectionType with entity element type. You can only include a path for a query with one of these result types.

I managed to find few explanations why this is happening. Any help?

+4
source share
2 answers

Product Localization? , 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();
+1

. , "", Include ( "" ).

, , "" .

:

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

var prods = myQuery.ToList();

:

var customerProds = prods.Select(i => {
                        var item = new { i.Product, i.Localization };
                        item.Product.Customer = i.Customer;
                        return item;
                    })
                    .ToList();
0

Source: https://habr.com/ru/post/1627301/


All Articles