• Register
0 votes
181 views

Problem :

I have one entity type called product which is generated by the entity framework. I have tried to write below query

public IMyQueryable<Product> GetProducts(int myCategoryID)
{
    return from prod in db.Products
    where prod.CategoryID== myCategoryID
    select new Product { Name = prod.Name};
}

My below written code throws a following error :

var myproducts = myproductRepository.GetProducts(1).Tolist();

 

“The entity or complex type Shop.Product cannot be constructed in a LINQ to Entities query”

However when I try to use select p instead of the long one select new Product { Name = p.Name}; it miraculously works correctly.

How can I perform the custom select section?

3.9k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

You simply cannot (and must not be able to) project onto the mapped entity. You will but, you will need to project onto the anonymous type or onto the DTO:

public class MyProductDTO
{
    public string MyName { get; set; }
    // Other fields which you may need from a Product entity
}

And now your method will return you the List of DTO's as shown in below code snippet.

public List<MyProductDTO> GetProducts(int myCategoryID)
{
    return (from prod in db.Products
    where prod.CategoryID == myCategoryID
    select new MyProductDTO { MyName = prod.Name }).ToList();
}

Hope above explanation will clear your doubts.

38.6k points

Related questions

1 vote
1 answer 37 views
37 views
Problem: Need help with the usage of entities , I am facing this issue. The entity or complex type cannot be constructed in a linq to entities query.
asked Mar 26 PkGuy 25.9k points
0 votes
1 answer 75 views
75 views
Problem: I need some suggestions about this problem,&ldquo;The specified type member is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported&rdquo;.
asked Apr 17 Moumita Bhowmick 12.9k points
0 votes
1 answer 28 views
28 views
Problem: Can anyone tell me the solution : Validation failed for one or more entities. see 'entityvalidationerrors' property for more details.?
asked Jun 10 Priscilla Gurpreet 74.7k points
0 votes
1 answer 62 views
62 views
Problem: Try to discover this: The entity type 'identityuserlogin<string>' requires a primary key to be defined.
asked Jun 14 asha 121k points
0 votes
1 answer 49 views
49 views
Problem: I am trying to save Employee details, which has references with City. But every time I try to save my contact, which is validated I get the exception: "ADO.Net Entity Framework An entity object cannot be referenced by multiple instances of IEntityChangeTracker"
asked Apr 25 ummeshani 10.8k points
0 votes
2 answers 661 views
661 views
Problem : Currently I am migrating from the Identity 1.0.0 to Identity 2.0.1. But the migrations code generated is nothing about the new IdentityUser. And It does not even add the new columns. So I tried to make the new project and also I ... current context At below line IdentityResult result = await UserManager.CreateAsync(user, model.Password); Any clue about the solution on my above problem?
asked Dec 25, 2019 alecxe 7.5k points
0 votes
1 answer 7 views
7 views
Problem: Mvc entity framework code first sample
asked Jul 24 nila 121k points
0 votes
1 answer 7 views
7 views
Problem: Entity framework code first one to many.
asked Jul 21 Rada Gracelynn 104k points
0 votes
1 answer 12 views
12 views
Problem: Entity framework many to many code first
asked Jul 18 Chi Omega 168k points
0 votes
1 answer 11 views
11 views
Problem: Entity framework code first many to many
asked Jul 18 Chi Omega 168k points