5

We are investigating the Entity Framework to see if it will meet our particular needs. Here is the scenario I am interested in:

I have a large table (let's call it VeryWideRecord) which has many columns, and it has a corresponding business object (it's also called VeryWideRecord). I would like to be able to query my database for a VeryWideRecord business object, but only have values for certain columns returned by the underlying SQL. Can I do this with the Entity Framework?

I am uncertain as to whether this could be done with the Entity Framework's table splitting feature, because the application needs to be able (at runtime) to change the columns that are requested. The reason for this is that we are trying to minimize the amount of information that is going across the wire.

I see how this could be done using NHibernate (example), but how can I do this with the Entity Framework?

Community
  • 1
  • 1
David McClelland
  • 2,686
  • 4
  • 28
  • 37

2 Answers2

0

It looks like you can achieve what you are looking for with entity SQL. Following is the link:

http://msdn.microsoft.com/en-us/library/bb738683.aspx

Pradeep
  • 3,258
  • 1
  • 23
  • 36
Brian Ball
  • 12,268
  • 3
  • 40
  • 51
0

Something like this (in VB.NET:

Dim pl As New List(Of VeryWideRecord)(
     (From p In db.VeryWideRecords 
      Select New With {.RecID = p.RecID}
     ).ToList().Select(
       Function(r)
         Return New VeryWideRecord With {.RecID = r.RecID}
       End Function))

With this you have some compiletime checking. But if you want to do this really dynamically, eSQL is maybe more suitable. And also, it's not really the Entity Framework, but the lambda expression who does the trick.

MarcelDevG
  • 1,377
  • 10
  • 10
  • I was going to suggest the projection like you did, but the question stated that the columns that are requested will be determined at runtime, not compile time. Also, FYI, you can have EF do the work of the proejction, you just wouldn't want to call `ToList()` and the projection should return an anonymous type. It should be noted that there are limitations to what you can do in a projection that is handled by EF (you generally can't call functions within the projection). In the case of doing the projection w/o EF, then using `AsEnumerable()` is preferred over `ToList()` – Brian Ball Feb 12 '11 at 00:19
  • Agreed, AsEnumerable() would be better, but is was more an example for showing the lambda part for creating the record. As for returning an Anynomous type, that's what he didn't want. – MarcelDevG Feb 15 '11 at 09:16