4

i have this code in my controller

IQueryable<SellingItem> sellingitems = db.SellingItems
            .GroupBy(s => new { s.ItemId ,s.Condition,s.ExpInDays})
            .Select(si => new SellingItem
            {
                Item = si.First().Item,
                Condition = si.First().Condition,
                ExpInDays = si.First().ExpInDays,
                Qty = si.Sum(i => i.Qty),
            });

and when i try to run it i get an error

The entity or complex type cannot be constructed in a LINQ to Entities query

now it look to me that my linq query is too complex for entity framework to handle,

so i have 2 workarounds, but i don't like both of them.

1. load the whole table in memory and make the query like this

2. use a SQL statement, which will be faster but will not follow the entity framework rules

is there a better way?

-----UPDATE---------------------------------

as it turn out (thanks a lot guys)

i was wrong by stating

now it look to me that my linq query is too complex for entity framework to handle,

and it didn't work because of the fact that i used the same class for the result. i created a new class and now it works amazing!!

CMS
  • 3,657
  • 1
  • 27
  • 46
  • Can you post your workarounds so that we can see what 'solved' the problem for you? – Luke Aug 10 '15 at 13:18
  • i didnt write the code yet its just im trying to figure out which workaround to use, but i will add them soon – CMS Aug 10 '15 at 13:20
  • 1
    possible duplicate of [The entity cannot be constructed in a LINQ to Entities query](http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities-query) – cverb Aug 10 '15 at 13:21
  • no i come from there read my question, i dont ask what the error is, i ask if my workaround is good – CMS Aug 10 '15 at 13:24
  • Your statement of `now it look to me that my linq query is too complex for entity framework to handle` is completely incorrect. – Luke Aug 10 '15 at 13:28
  • Did you even read the answer to the question? The accepted answer overthere is for me the only correct 'workaround' to solve this problem. And also as @Coulton said, your statement is completely wrong (also explained in the accepted answer of the question I posted "You cannot (and should not be able to) project onto a mapped entity"). – cverb Aug 10 '15 at 13:38
  • 1
    yup now i got it im middle of writing a new class for the results hope it will work then will come back here update, thanks all !! – CMS Aug 10 '15 at 13:42

1 Answers1

4

You don't need to resort to any of your workarounds to fix that exception per se. The problem is that SellingItem is a class that is part of your Entity Framework model. The reason for this is explained in the comments on this answer.

Either select an anonymous object like so:

IQueryable<SellingItem> sellingitems = db.SellingItems
.GroupBy(s => new { s.ItemId ,s.Condition,s.ExpInDays})
.Select(si => new
{
    Item = si.First().Item,
    Condition = si.First().Condition,
    ExpInDays = si.First().ExpInDays,
    Qty = si.Sum(i => i.Qty),
});

Or create an object specifically for the select that you are trying to do:

public class NewClass
{
    public ItemClass Item { get;set; }
    public ConditionClass Condition { get;set; }
    public in ExpInDays { get;set; }
    public int Qty { get;set; }
}

Naturally you will need to make sure that the types in this specific class match up to their respective types.

You can then use the new class to do the select:

 // Use new class
 IQueryable<SellingItem> sellingitems = db.SellingItems
.GroupBy(s => new { s.ItemId ,s.Condition,s.ExpInDays})
.Select(si => new NewClass
{
    Item = si.First().Item,
    Condition = si.First().Condition,
    ExpInDays = si.First().ExpInDays,
    Qty = si.Sum(i => i.Qty),
});
Community
  • 1
  • 1
Luke
  • 22,826
  • 31
  • 110
  • 193
  • why cant i use the same class instead NewClass? – CMS Aug 10 '15 at 13:28
  • For the reason in the comments on [this answer](http://stackoverflow.com/a/5325861/894792), as I stated in the answer :) – Luke Aug 10 '15 at 13:29
  • In short, it's because `SellingItem` represents an entire table, if you only need selected pieces of data you should create a class that matches your specific use case... or just use an anonymous object which is simple enough. – Luke Aug 10 '15 at 13:30
  • 1
    Good luck my friend. Hope that I've helped! – Luke Aug 10 '15 at 13:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/86616/discussion-between-cms-and-coulton). – CMS Aug 10 '15 at 14:00