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 to Entities 1 to many query

LINQ to Entities 1 to many query

Scheduled Pinned Locked Moved LINQ
databasehelpcsharplinqquestion
3 Posts 2 Posters 9 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.
  • P Offline
    P Offline
    Paul E Davies
    wrote on last edited by
    #1

    Hi All, I'm currently into my first project using LINQ and Entities and fairly early on in the project I'm stumbling. I have two tables and I've created a Data Access project which contains and edmx file containing objects which map to these tables. I can use this to insert into the DB fine. The structure is such that table Event has an EventID column and this joins to a table EventAttribute on EventAttribute.EventID and there can be many EventAttributes for each event. What I want to do is query the DB using my entities to return the first 1 event based on the lowest EventID and all EventAttributes for that Event. what I have at the moment is

    using (AuditLoggerEntities ale = new AuditLoggerEntities())
    {
    var e = from Ev in ale.Events join EventAttribute in ale.EventAttributes on Ev.EventID equals EventAttribute.EventID select Ev;

     // other stuff
    

    }

    but this (obviously) returns all events and associated EventAttributes but by limiting this using Take(1) also limits EventAttributes to the top 1. So after much googling and trying various options I'm stuck so can someone help me write a query that returns 1 event and all Associated EventAttributes? Thanks Paul

    M 1 Reply Last reply
    0
    • P Paul E Davies

      Hi All, I'm currently into my first project using LINQ and Entities and fairly early on in the project I'm stumbling. I have two tables and I've created a Data Access project which contains and edmx file containing objects which map to these tables. I can use this to insert into the DB fine. The structure is such that table Event has an EventID column and this joins to a table EventAttribute on EventAttribute.EventID and there can be many EventAttributes for each event. What I want to do is query the DB using my entities to return the first 1 event based on the lowest EventID and all EventAttributes for that Event. what I have at the moment is

      using (AuditLoggerEntities ale = new AuditLoggerEntities())
      {
      var e = from Ev in ale.Events join EventAttribute in ale.EventAttributes on Ev.EventID equals EventAttribute.EventID select Ev;

       // other stuff
      

      }

      but this (obviously) returns all events and associated EventAttributes but by limiting this using Take(1) also limits EventAttributes to the top 1. So after much googling and trying various options I'm stuck so can someone help me write a query that returns 1 event and all Associated EventAttributes? Thanks Paul

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      how about...

      var e = ale.Events.OrderBy(e => e.EventID).First().EventAttributes;

      e will now be a list of EventAttributes for the first Event when events are ordered ascending by EventID Also, if EventID is an AutoIncrement you can assume the entries will be in ascending order anyway and probably just leave out the OrderBy part...

      var e = ale.Events.First().EventAttributes;

      I may or may not be responsible for my own actions

      P 1 Reply Last reply
      0
      • M musefan

        how about...

        var e = ale.Events.OrderBy(e => e.EventID).First().EventAttributes;

        e will now be a list of EventAttributes for the first Event when events are ordered ascending by EventID Also, if EventID is an AutoIncrement you can assume the entries will be in ascending order anyway and probably just leave out the OrderBy part...

        var e = ale.Events.First().EventAttributes;

        I may or may not be responsible for my own actions

        P Offline
        P Offline
        Paul E Davies
        wrote on last edited by
        #3

        That's excellent thanks. The change to LINQ is a bit of a paradigm shift and I certainly need more practise! Thanks again for your help. Paul

        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