Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. LINQ Select specific records and

LINQ Select specific records and

Scheduled Pinned Locked Moved LINQ
databasecsharplinq
5 Posts 4 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 1 Offline
    1 Offline
    1 21 Gigawatts
    wrote on last edited by
    #1

    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. :((

    1 R B 3 Replies Last reply
    0
    • 1 1 21 Gigawatts

      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. :((

      1 Offline
      1 Offline
      1 21 Gigawatts
      wrote on last edited by
      #2

      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.

      P 1 Reply Last reply
      0
      • 1 1 21 Gigawatts

        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.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        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.

        My blog | My articles

        1 Reply Last reply
        0
        • 1 1 21 Gigawatts

          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. :((

          R Offline
          R Offline
          Roger Alsing 0
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • 1 1 21 Gigawatts

            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. :((

            B Offline
            B Offline
            balaji t
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups