0

I have a database constructed using code first. I found that querying using projection into a concrete type is by far the fastest method, faster than using Include statements. I run into the following problem however:

dim records=(From record in db.SomeDbSet
             Where record.UserID=userID
             Select New UserSpecificRecord With
             { .Name=record.User.Name
               .Tasks=record.Tasks
             }).ToList

I get different errors when the .Tasks= part is executed, ranging from invalid cast exceptions to messages that the Enity Framework does not support this kind of querying, depending on the type of collection I make the Tasks property of the UserSpecificRecord class.

When I change the code to:

 Dim records= (From record in db.SomeDbSet
                 Where record.UserID=userID
                 Select New With
                 { .Name=record.User.Name
                   .Tasks=record.Tasks
                 }).ToList

   dim userRecords=(From record in records
                    Select New UserSpecificRecord With
                    { .Name=record.Name
                     .Tasks=record.Tasks
                     }).ToList

i.e.: I project to an anonymous type and then build the concrete type it all works. I would very much like to be able to project to the concrete type directly though. Can it be done?

Dabblernl
  • 15,831
  • 18
  • 96
  • 148

1 Answers1

1

You cannot project to an 'entity' (is your UserSpecificRecord mapped as well? seems so by the name).
It's not about the 'concrete class`.

See this answer The entity cannot be constructed in a LINQ to Entities query

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
  • 'UserSpecifiedRecord' is no entity just a class to conveniently aggregate the data I need to work with. – Dabblernl Mar 31 '13 at 18:13
  • It's hard to say like this w/o a code. Could you post your scaled-down model (entities involved), so I could run - and your 'concrete' class as well. And I'm actually suspecting it could be something entirely unrelated (as you mention range of errors), and EF related. btw. do you use code-first or db-first – NSGaga-mostly-inactive Mar 31 '13 at 18:22
  • It should be clear enough I think: there are three entities: Records, User and Task, a Record has many Tasks, a User has many Records. The UserSpecificRecord is a DTO. – Dabblernl Mar 31 '13 at 18:30
  • Ok, thanks for checking it out for me, I will investigate further. – Dabblernl Apr 01 '13 at 07:51
  • Thanks for working with me. It did work after all. The problem was related to inherited entities and type conversions. Moreoever, the only type of collection that can be directly projected seems to be the IENumerable(Of TEntity) – Dabblernl Apr 02 '13 at 06:22
  • you're welcome - that's probably the 'covariance' then - `IEnumerable` is defined as `out` / covariant and supports upcasting - from e.g. IEnumerable to IEnumerable - while with other interfaces (it can only be interface, even less for classes e.g. List<>) doesn't work, unless you define it specifically. If interested, you can see some explanations I made [here](http://stackoverflow.com/a/15599634/417747) or [here](http://stackoverflow.com/a/15723829/417747). All the best. – NSGaga-mostly-inactive Apr 02 '13 at 11:55