Geeks With Blogs
Title Of Blog

Skip to the bottom if want the quick answer ;-)

Just a quick note on something I ran into today...

I've taken to utilizing a View Model in which objects are created on a 1 to 1 basis to hold entities supplying data to the View/UI.  As such, I modified my existing linq queries such that the View Model objects are created in the select portion of the query resulting in an enumerable list I can supply to the View.  Below is an example:

IQueryable<sessionfoo> fooquery = ((IEnumerable<sessionfoo>)Session["foo"]).AsQueryable();

IQueryable<fooViewModel> result = from f in fooquery where f.isFooey == true select new fooViewModel(f); 

I am relatively new to the Entity Framework, so it is totally possible there is a better way of accomplishing all this, however, it illustrates the point.  You can see above that I am pulling out my list of entity objects from session and dumping them into an IQueryable instance.  From this point, I execute a Linq to Entity query which allows me to utilize a parameterized constructor in the select.

Later in the day I tried this same approach with a query which was Ling to SQL and that's where I was promptly informed by the runtime that this is not permissible. If you need to get around this inconvenience simply call ToList() on your query, then call AsQueryable() which now allows you to execute Linq to Entity queries on the original Linq to SQL result. 

So, you can use parameterized constructors for Linq to Objects, but not for Linq to Entities

Posted on Thursday, September 22, 2011 3:09 PM C# , SQL , Entity Framework | Back to top


Comments on this post: Entity Framework 4 - When you can use Parameterized Constructors in Select

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Jake Rutherford | Powered by: GeeksWithBlogs.net