• Register
0 votes
82 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?

7 5 2
3,870 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.

9 7 4
38,600 points

Related questions

0 votes
2 answers 296 views
296 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 108 views
108 views
Problem : I am getting bellow error related to datetime the conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
asked Nov 7, 2019 peterlaw 6.9k points
0 votes
1 answer 13 views
13 views
How can i apply any entity framework approach like code first and database first in the ASP.NET MVC application.
asked Sep 16, 2020 Daniel Anderson 4k points
0 votes
1 answer 28 views
28 views
Problem: Currently I have the table called Student in the database with some of the values and I am trying to display the values in the webgrid with the help of entityframework. I have done exactly the same kind of work before many times using the Ado.net, but ... I assign the VAR value to List also is this my correct approach? Kindly guide me in fixing my problem related to Entity Framework.
asked Jul 3, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 694 views
694 views
Problem : I am having the table with the name EducationTypes and also an Entity named as EducationType, I just tried to rename the one of the entity properties but now I am always facing following error : Either the parameter @objname is ambiguous or the ... SQL Script is as below: EXECUTE sp_rename @objname = N'dbo.EducationTypes.nvarchar', @newname = N'EducationTypeTitle', @objtype = N'COLUMN'
asked Dec 9, 2019 alecxe 7.5k points
0 votes
1 answer 74 views
74 views
Problem : My Query is giving the strange error as follows there is already an open datareader associated with this command which must be closed first.
asked Nov 15, 2019 peterlaw 6.9k points
0 votes
1 answer 13 views
13 views
I learnt entity framework just want to do some quiz. Where should i start? Can you provide some interview questions on entity framework. THank-you!
asked Dec 3, 2020 Lalit Kumar 360 points
0 votes
1 answer 67 views
67 views
When trying to create a user using claim identity asp.net this error showed while creating claims identity user ApplicationUser user = new ApplicationUser { EmailConfirmed = true, UserName = model.myUser.Email, Email = model.myUser.Email , PhoneNumber = model.myUser.PhoneNumber, ... model.myUser.Email, Email = model.myUser.Email , }; it worked perfectly, so i want to know what is wrong.
asked Oct 30, 2020 psandprop 2.4k points
0 votes
1 answer 318 views
318 views
Problem : I am getting into the C# coding but I am having following issue: I have below code on the UserControl: using MyFirstDataLayer.Section1; public class MyFirstClass { protected void MyFirstMethod { MyFirstClass.MyFirstItem oItem = new MyFirstClass.MyFirstItem(); oItem = ... name instead I am not sure of what this actually means, I did some research but was unable to figure it out.
asked Dec 18, 2019 alecxe 7.5k points