LINQ Select specific records and
-
Hi peeps I must be suffering from that rare stupidity based disease 'code blindness', because I can't figure out why the code below doesn't work. I'm trying to return specific records from a SQL table 'QuickLinks'. If I use the query below to select all, everything works fine:
var query1 = (from QuickLink lnk in datb.QuickLinks where lnk.LinkID == checkDataLink.LinkID select lnk).Single();
Now, if I use the 'select new' statement to specify what records I actually need, like so:var query = (from QuickLink lnk in datb.QuickLinks where lnk.LinkID == checkDataLink.LinkID select new QuickLink() { LinkID = lnk.LinkID, Url = lnk.Url, Title = lnk.Title, }).Single();
It throws a NotSupportedException with the message "Explicit construction of entity type 'QuickLink' in query is not allowed. What should I be doing, beacuse everything I've tried so far has just failed. Boo-hoo. :(( -
Hi peeps I must be suffering from that rare stupidity based disease 'code blindness', because I can't figure out why the code below doesn't work. I'm trying to return specific records from a SQL table 'QuickLinks'. If I use the query below to select all, everything works fine:
var query1 = (from QuickLink lnk in datb.QuickLinks where lnk.LinkID == checkDataLink.LinkID select lnk).Single();
Now, if I use the 'select new' statement to specify what records I actually need, like so:var query = (from QuickLink lnk in datb.QuickLinks where lnk.LinkID == checkDataLink.LinkID select new QuickLink() { LinkID = lnk.LinkID, Url = lnk.Url, Title = lnk.Title, }).Single();
It throws a NotSupportedException with the message "Explicit construction of entity type 'QuickLink' in query is not allowed. What should I be doing, beacuse everything I've tried so far has just failed. Boo-hoo. :((For anyone else out there with a similar problem, I don't think you can do it. From what I've read - and talked about with various other people in other forums - you pretty much have to get the entire row. LINQ doesn't like partly filling Entities. Either that, or use an anonymous type - but you then can't cast it back to the type you require (which is what I want to do). If anyone out there can prove me wrong I'll be glad to hear it, as I can't beleive that you HAVE to drag up all the data from an entire row even if you just want 3 columns. What a waste of resource.
-
For anyone else out there with a similar problem, I don't think you can do it. From what I've read - and talked about with various other people in other forums - you pretty much have to get the entire row. LINQ doesn't like partly filling Entities. Either that, or use an anonymous type - but you then can't cast it back to the type you require (which is what I want to do). If anyone out there can prove me wrong I'll be glad to hear it, as I can't beleive that you HAVE to drag up all the data from an entire row even if you just want 3 columns. What a waste of resource.
The reason that Linq doesn't support this has a lot to do with items like change tracking. The way that entity usage is supposed to happen is that they are created outside of queries and inserted via the DataContext and then returned back via queries. They are never meant to be created by query projections. Also, there is an issue with whether or not projection entities should be cached or changed tracks.
Deja View - the feeling that you've seen this post before.
-
Hi peeps I must be suffering from that rare stupidity based disease 'code blindness', because I can't figure out why the code below doesn't work. I'm trying to return specific records from a SQL table 'QuickLinks'. If I use the query below to select all, everything works fine:
var query1 = (from QuickLink lnk in datb.QuickLinks where lnk.LinkID == checkDataLink.LinkID select lnk).Single();
Now, if I use the 'select new' statement to specify what records I actually need, like so:var query = (from QuickLink lnk in datb.QuickLinks where lnk.LinkID == checkDataLink.LinkID select new QuickLink() { LinkID = lnk.LinkID, Url = lnk.Url, Title = lnk.Title, }).Single();
It throws a NotSupportedException with the message "Explicit construction of entity type 'QuickLink' in query is not allowed. What should I be doing, beacuse everything I've tried so far has just failed. Boo-hoo. :((Linq to sql supports load spans, so you can define that you only want to eager load those fields. however, the load span feature in linq to sql is per DataContext, so you cannot change it per query. (it _is_ possible , but only with hacks.. I know Patrik Löwendahl http://www.lowendahl.net[^] got some semi working hack for it, its in there somewhere on his blog)
Blog: http://www.rogeralsing.com Projects: http://www.puzzleframework.com
-
Hi peeps I must be suffering from that rare stupidity based disease 'code blindness', because I can't figure out why the code below doesn't work. I'm trying to return specific records from a SQL table 'QuickLinks'. If I use the query below to select all, everything works fine:
var query1 = (from QuickLink lnk in datb.QuickLinks where lnk.LinkID == checkDataLink.LinkID select lnk).Single();
Now, if I use the 'select new' statement to specify what records I actually need, like so:var query = (from QuickLink lnk in datb.QuickLinks where lnk.LinkID == checkDataLink.LinkID select new QuickLink() { LinkID = lnk.LinkID, Url = lnk.Url, Title = lnk.Title, }).Single();
It throws a NotSupportedException with the message "Explicit construction of entity type 'QuickLink' in query is not allowed. What should I be doing, beacuse everything I've tried so far has just failed. Boo-hoo. :((hi dude, try like this for a specific record. var query1 = (from QuickLink lnk in datb.QuickLinks where lnk.LinkID == checkDataLink.LinkID select new {lnk.linkid,lnk.url} then give like string linkid = query1.Select(a=>a.lnkid).single(); now linkid is taken explicitly
T.Balaji