ChatGPT解决这个技术问题 Extra ChatGPT

The entity cannot be constructed in a LINQ to Entities query

There is an entity type called Product that is generated by entity framework. I have written this query

public IQueryable<Product> GetProducts(int categoryID)
{
    return from p in db.Products
           where p.CategoryID== categoryID
           select new Product { Name = p.Name};
}

The code below throws the following error :

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

var products = productRepository.GetProducts(1).Tolist();

But when I use select p instead of select new Product { Name = p.Name}; it works correctly.

How can I preform a custom select section?

System.NotSupportedException: 'The entity or complex type 'StudentInfoAjax.Models.Student' cannot be constructed in a LINQ to Entities query.'
You can map it to dto as in accepted answer below, but also make sure that this model is not used in the DB context, otherwise you will get the same error.

O
Ogglas

You cannot (and should not be able to) project onto a mapped entity. You can, however, project onto an anonymous type or onto a DTO:

public class ProductDTO
{
    public string Name { get; set; }
    // Other field you may need from the Product entity
}

And your method will return a List of DTO's.

public List<ProductDTO> GetProducts(int categoryID)
{
    return (from p in db.Products
            where p.CategoryID == categoryID
            select new ProductDTO { Name = p.Name }).ToList();
}

I don't understand why I should not be able to do this... This would be very usefull...
Well, mapped entities in EF basically represent database tables. If you project onto a mapped entity, what you basically do is partially load an entity, which is not a valid state. EF won't have any clue how to e.g. handle an update of such an entity in the future (the default behaviour would be probably overwriting the non-loaded fields with nulls or whatever you'll have in your object). This would be a dangerous operation, since you would risk losing some of your data in the DB, therefore it is not allowed to partially load entities (or project onto mapped entities) in EF.
@Yakimych that makes sense except if you have some aggregate entity that you are generating/creating via a query and therefore are fully aware/intend to create a brand new entity that you will then manipulate and later add. In this case you either have to force run the query or push into a dto and back into an entity to add - which is frustrating
@Cargowire - I agree, that scenario exists, and it is frustrating when you know what you are doing but are not allowed to do it due to limitations. However, had this been allowed, there would be lots of frustrated developers complaining about their data getting lost when e.g. trying to save partially loaded entities. IMO, an error that blows up with a lot of noise (throwing an exception, etc) is better than behavior which can cause hidden bugs that are difficult to track down and explain (things kind of work nicely before you start noticing missing data).
F
Farzad Karimi

You can project into anonymous type, and then from it to model type

public IEnumerable<Product> GetProducts(int categoryID)
{
    return (from p in Context.Set<Product>()
            where p.CategoryID == categoryID
            select new { Name = p.Name }).ToList()
           .Select(x => new Product { Name = x.Name });
}

Edit: I am going to be a bit more specific since this question got a lot of attention.

You cannot project into model type directly (EF restriction), so there is no way around this. The only way is to project into anonymous type (1st iteration), and then to model type (2nd iteration).

Please also be aware that when you partially load entities in this manner, they cannot be updated, so they should remain detached, as they are.

I never did completely understand why this is not possible, and the answers on this thread do not give strong reasons against it (mostly speaking about partially loaded data). It is correct that in partially loaded state entity cannot be updated, but then, this entity would be detached, so accidental attempts to save them would not be possible.

Consider method I used above: we still have a partially loaded model entity as a result. This entity is detached.

Consider this (wish-to-exist) possible code:

return (from p in Context.Set<Product>()
        where p.CategoryID == categoryID
        select new Product { Name = p.Name }).AsNoTracking().ToList();

This could also result in a list of detached entities, so we would not need to make two iterations. A compiler would be smart to see that AsNoTracking() has been used, which will result in detached entities, so it could allow us to do this. If, however, AsNoTracking() was omitted, it could throw the same exception as it is throwing now, to warn us that we need to be specific enough about the result we want.


This is the cleanest solution when you don't need / don't care about state of the selected entity you want to project.
And when you dont care if you return IEnumerable or IQueryable ;). But still you get my upvote cause this solution works for me now.
technically, the projection to the model type is occurring outside the query, and I believe also requires an additional iteration through the list. I won't use this solution for my code, but it is solution for the question. uptick.
I prefer this to the accepted DTO solution - much more elegant and clean
Except that, with respect, it's not actually an answer to the question. This is an answer as to how to do a Linq To Objects projection, not a Linq to Entities query projection. So the DTO option is the only option re: Linq to Entities.
T
Tomasz Iniewicz

There is another way that I found works, you have to build a class that derives from your Product class and use that. For instance:

public class PseudoProduct : Product { }

public IQueryable<Product> GetProducts(int categoryID)
{
    return from p in db.Products
           where p.CategoryID== categoryID
           select new PseudoProduct() { Name = p.Name};
}

Not sure if this is "allowed", but it works.


Clever! Tried this now and it works. I'm sure it will somehow burn me though.
BTW this does bite you if you try to persist the results of GetProducts() since EF cannot find the mapping for PseudoProduct e.g. "System.InvalidOperationException: Mapping and metadata information could not be found for EntityType 'blah.PseudoProduct'".
Best answer, and the only one that answers within the parameters of the question. All other answers change the return type or prematurely execute the IQueryable and use linq to objects
100% shocked it worked... in EF 6.1 this is working.
@mejobloggs Try [NotMapped] attribute on the derived class, or .Ignore if you're using the fluent API.
B
Bojan Hrnkas

Here is one way to do this without declaring aditional class:

public List<Product> GetProducts(int categoryID)
{
    var query = from p in db.Products
            where p.CategoryID == categoryID
            select new { Name = p.Name };
    var products = query.ToList().Select(r => new Product
    {
        Name = r.Name;
    }).ToList();

    return products;
}

However, this is only to be used if you want to combine multiple entities in a single entity. The above functionality (simple product to product mapping) is done like this:

public List<Product> GetProducts(int categoryID)
{
    var query = from p in db.Products
            where p.CategoryID == categoryID
            select p;
    var products = query.ToList();

    return products;
}

S
Suren

Another simple way :)

public IQueryable<Product> GetProducts(int categoryID)
{
    var productList = db.Products
        .Where(p => p.CategoryID == categoryID)
        .Select(item => 
            new Product
            {
                Name = item.Name
            })
        .ToList()
        .AsQueryable(); // actually it's not useful after "ToList()" :D

    return productList;
}

good point I just learned something IQueryable with your nice reply. It would have been nice though if you would have explained WHY it's not useful after a ToList() and the reason is that you can't use generic lists in a LINQ-to-SQL query. So if you know you're always gonna push the results into another query by the caller then certainly makes sense to be IQueryable. But if not...if you are gonna use it as a generic list after, then use the ToList() inside the method so you aren't doing a ToList() on the IQueryable each and every call to this method.
You totally alright my friend.I just imitate the question method signature, because of that I convert it to a Query-able... ;)
This works, the productList becomes uneditable after the ToList(). How can I make it editable?
If you put .ToList in query, it is executed and pulled data from server then what is the point to make it again AsQueryable?.
@Moshii just in order to satisfy the method return type signature, (as I said in the answer, it's not useful anymore).
D
Draken

You can use this and it should be working --> You must use toList before making the new list using select:

db.Products
    .where(x=>x.CategoryID == categoryID).ToList()
    .select(x=>new Product { Name = p.Name}).ToList(); 

This would however still do a 'SELECT * FROM [..]', not a 'SELECT name FROM [..]'
C
Community

In response to the other question which was marked as duplicate (see here) I figured out a quick and easy solution based on the answer of Soren:

data.Tasks.AddRange(
    data.Task.AsEnumerable().Select(t => new Task{
        creator_id   = t.ID,
        start_date   = t.Incident.DateOpened,
        end_date     = t.Incident.DateCLosed,
        product_code = t.Incident.ProductCode
        // so on...
    })
);
data.SaveChanges();

Note: This solution only works if you have a navigation property (foreign key) on the Task class (here called 'Incident'). If you don't have that, you can just use one of the other posted solutions with "AsQueryable()".


J
Jelman

You can solve this by using Data Transfer Objects (DTO's).

These are a bit like viewmodels where you put in the properties you need and you can map them manually in your controller or by using third-party solutions like AutoMapper.

With DTO's you can :

Make data serialisable (Json)

Get rid of circular references

Reduce networktraffic by leaving properties you don't need (viewmodelwise)

Use objectflattening

I've been learning this in school this year and it's a very useful tool.


v
vikas suhag

If you are using Entity framework, then try removing property from DbContext which uses your complex model as Entity I had same problem when mapping multiple model into a viewmodel named Entity

public DbSet<Entity> Entities { get; set; }

Removing the entry from DbContext fixed my error.


B
Basheer AL-MOMANI

if you are Executing Linq to Entity you can't use the ClassType with new in the select closure of query only anonymous types are allowed (new without type)

take look at this snippet of my project

//...
var dbQuery = context.Set<Letter>()
                .Include(letter => letter.LetterStatus)
                .Select(l => new {Title =l.Title,ID = l.ID, LastModificationDate = l.LastModificationDate, DateCreated = l.DateCreated,LetterStatus = new {ID = l.LetterStatusID.Value,NameInArabic = l.LetterStatus.NameInArabic,NameInEnglish = l.LetterStatus.NameInEnglish} })
                               ^^ without type__________________________________________________________________________________________________________^^ without type

of you added the new keyword in Select closure even on the complex properties you will got this error

so remove the ClassTypes from new keyword on Linq to Entity queries ,,

because it will transformed to sql statement and executed on SqlServer

so when can I use new with types on select closure?

you can use it if you you are dealing with LINQ to Object (in memory collection)

//opecations in tempList , LINQ to Entities; so we can not use class types in select only anonymous types are allowed
var tempList = dbQuery.Skip(10).Take(10).ToList();// this is list of <anonymous type> so we have to convert it so list of <letter>

//opecations in list , LINQ to Object; so we can use class types in select
list = tempList.Select(l => new Letter{ Title = l.Title, ID = l.ID, LastModificationDate = l.LastModificationDate, DateCreated = l.DateCreated, LetterStatus = new LetterStatus{ ID = l.LetterStatus.ID, NameInArabic = l.LetterStatus.NameInArabic, NameInEnglish = l.LetterStatus.NameInEnglish } }).ToList();
                                ^^^^^^ with type 

after I executed ToList on query it became in memory collection so we can use new ClassTypes in select


Sure you can use anonymous types, but you cannot create an entity within the LINQ query, even to set an anonymous member, because LINQ-to-Entities still throws the same exception.
S
Sterling Diaz

In many cases, the transformation is not needed. Think for the reason you want the strongly type List, and evaluate if you just want the data, for example, in a web service or for displaying it. It does not matter the type. You just need to know how to read it and check that is identical to the properties defined in the anonymous type that you defined. That is the optimun scenario, cause something you don't need all the fields of an entity, and that's the reason anonymous type exists.

A simple way is doing this:

IEnumerable<object> list = dataContext.Table.Select(e => new { MyRequiredField = e.MyRequiredField}).AsEnumerable();

v
vapcguy

It won't let you map back onto Product since that is your table you are querying. You need an anonymous function, then you can add it to a ViewModel, and add each ViewModel to a List<MyViewModel> and return these. It's a slight digression, but I include caveats about handling nullable dates because these are a pain in the behind to deal with, just in case you have any. This is how I handled it.

Hopefully you have a ProductViewModel:

public class ProductViewModel
{
    [Key]
    public string ID { get; set; }
    public string Name { get; set; }
}

I have dependency injection/repository framework where I call a function to grab my data. Using your post as an example, in your Controller function call, it would look like this:

int categoryID = 1;
var prods = repository.GetProducts(categoryID);

In the repository class:

public IEnumerable<ProductViewModel> GetProducts(int categoryID)
{
   List<ProductViewModel> lstPVM = new List<ProductViewModel>();

   var anonymousObjResult = from p in db.Products
                            where p.CategoryID == categoryID 
                            select new
                            {
                                CatID = p.CategoryID,
                                Name = p.Name
                            };

        // NOTE: If you have any dates that are nullable and null, you'll need to
        // take care of that:  ClosedDate = (DateTime?)p.ClosedDate ?? DateTime.Now

        // If you want a particular date, you have to define a DateTime variable,
        // assign your value to it, then replace DateTime.Now with that variable. You
        // cannot call a DateTime.Parse there, unfortunately. 
        // Using 
        //    new Date("1","1","1800"); 
        // works, though. (I add a particular date so I can edit it out later.)

        // I do this foreach below so I can return a List<ProductViewModel>. 
        // You could do: return anonymousObjResult.ToList(); here
        // but it's not as clean and is an anonymous type instead of defined
        // by a ViewModel where you can control the individual field types

        foreach (var a in anonymousObjResult)
        {                
            ProductViewModel pvm = new ProductViewModel();
            pvm.ID = a.CatID;  
            pvm.Name = a.Name;
            lstPVM.Add(rvm);
        }

        // Obviously you will just have ONE item there, but I built it 
        // like this so you could bring back the whole table, if you wanted
        // to remove your Where clause, above.

        return lstPVM;
    }

Back in the controller, you do:

 List<ProductViewModel> lstProd = new List<ProductViewModel>();

 if (prods != null) 
 {
    // For setting the dates back to nulls, I'm looking for this value:
    // DateTime stdDate = DateTime.Parse("01/01/1800");

    foreach (var a in prods)
    {
        ProductViewModel o_prod = new ReportViewModel();
        o_prod.ID = a.ID;
        o_prod.Name = a.Name;
       // o_prod.ClosedDate = a.ClosedDate == stdDate ? null : a.ClosedDate;
        lstProd.Add(o_prod);
    }
}
return View(lstProd);  // use this in your View as:   @model IEnumerable<ProductViewModel>

H
HamidReza

only add AsEnumerable() :

public IQueryable<Product> GetProducts(int categoryID)
{
    return from p in db.Products.AsEnumerable()
           where p.CategoryID== categoryID
           select new Product { Name = p.Name};
}

NEVER do it! This will fetch all data from DB and then will do the select.
This is why in some companies Linq is forbidden to use.
H
HamidReza

you can add AsEnumerable to your collection like the follow :

public IQueryable<Product> GetProducts(int categoryID)
{
    return from p in db.Products.AsEnumerable()
           where p.CategoryID== categoryID
           select new Product { Name = p.Name};
}

Why this is a bad answer although it does work... .AsEnumerable ends linq to entities. The Where clause and everything else is handled outside of linq to Entities. ie every product is retrieved then filtered by linq to objects. Aside from this it is pretty much exactly the same as the .ToList answer above. stackoverflow.com/questions/5311034/…
The problem with this is that is just a select * from... performed, not select new Product { Name = p.Name}, since you will get also a cyclic reference. And you want just the Name.

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now