views:

236

answers:

3

When creating a query with EF, Normally we will create an anonymous type in order to limit the number of columns returned.

But anonymous type cannot be returned or used as a parameter to a method call, which means all work related to that anonymous object should be done inside a single method. This is really bad.

And certainly, we don't want to create explicit types just to represent a subset of an existing entity.

In my point of view, we still wanna play with the existing entity (like Person), but in different scenarios, we just care about certain properties. So I believe the best way is to partially populate an entity. But it seems Linq 2 EF does not support it.

Any suggestions?

Thanks

+1  A: 

Not specific to EF (and we were not using EF), but I frequently used what we called "digest" types. These contained primary key information and a subset of other vital information, but were readonly and could be used to build a full entity. We also used lazy loading in our regular types, but this was a specific optimization. Typically used for search results - e.g. Patient objects are large, but Patient Digests contain the basic information to identify the patient in a set of (potentially hundreds of Britneys and Michaels) search results: Last, First, Chart Number, DOB. We did not allow the digest objects to grow without specifically addressing the absolute need to expand the properties.

Cade Roux
A: 

You can return an anonymous type as System.Object, else you have to "create explicit types to represent a subset of an existing entity". What I would do is create a public interface with a subset of the properties of the entity type, and then make the entity type and the subset type both implement it. The subset type can be internal so your application code only sees the entity type and the interface.

Max Toro
+1  A: 
And certainly, we don't want to create explicit types just to represent a subset of an existing entity.

I don't think there is a way to work around it. If you domain model has small set of properties than the EF generated types, then just use LINQ to project the EF model to your domain model. It's actually not that bad, because you are not relying on ET model directly, and it's easier to do TDD.

J.W.
There is a way: http://blogs.msdn.com/alexj/archive/2009/04/25/tip-15-how-to-avoid-loading-unnecessary-properties.aspx But I generally prefer materializing POCO presentation models rather than partially-populated, "psuedo-entities."
Craig Stuntz
I guess you guys are right. Mapping to a domain model is the only way.But with POCO templates in EF CTP, you don't have your EF model anymore, only the POCO model. Which means I have to do something like this:var anonymousPersons = ctx.Persons.Select(p => new { p.Name, p.Phone }).ToList();var partialPersons = anonymousPersons.Select(p => new Person { Name = p.Name, p.Phone });And Craig, do you think it's the best way to do it?Thanks
Nan Li
Craig has a nice presentation on this topic, watch it, it's very good.http://blogs.teamb.com/craigstuntz/2009/09/11/38394/
J.W.