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. C#
  4. Linq To SQL Join Question

Linq To SQL Join Question

Scheduled Pinned Locked Moved C#
databasequestioncsharplinqhelp
3 Posts 3 Posters 0 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.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I have this method that returns a List. Each start date can have 1-n revisions. So after I get the start dates I then loop through them all and get the revisions for each one. My question is this... is it possible to somehow do this all in one query without having to loop and get the child records? Can I somehow select the joined records into entities in the same query as the start dates?

    public List GetJobStartDates(int jobId)
    {
    using (var dc = GetDataContext())
    {
    var results = (from jsd in dc.JobStartDates
    where jsd.JobId == jobId
    select new JobStartDateEntity
    {
    Id = jsd.Id,
    StartDateId = jsd.StartDateId ?? 0,
    JobId = jobId,
    ProjectStartDateId = jsd.ProjectStartDateId,
    Caption = jsd.Caption,
    Description = jsd.Description,
    Sequence = jsd.Sequence,
    DaysOffset = jsd.DaysOffset,
    StartDate = jsd.StartDate
    }).ToList();

    foreach (var result in results)
    {
        var startDateRevisions = (from sdr in dc.JobStartDateRevisions
                                    where sdr.Id == result.Id
                                    select new JobStartDateRevisionEntity
                                    { 
                                        Id = sdr.Id,
                                        JobId = sdr.JobId,
                                        StartDate = sdr.StartDate,
                                        Revision = sdr.Revision
                                    }).OrderByDescending(x => x.Revision).ToList();
    
        result.StartDateRevisions = startDateRevisions;
    }
    
    return results;
    

    }
    }

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    R 1 Reply Last reply
    0
    • K Kevin Marois

      I have this method that returns a List. Each start date can have 1-n revisions. So after I get the start dates I then loop through them all and get the revisions for each one. My question is this... is it possible to somehow do this all in one query without having to loop and get the child records? Can I somehow select the joined records into entities in the same query as the start dates?

      public List GetJobStartDates(int jobId)
      {
      using (var dc = GetDataContext())
      {
      var results = (from jsd in dc.JobStartDates
      where jsd.JobId == jobId
      select new JobStartDateEntity
      {
      Id = jsd.Id,
      StartDateId = jsd.StartDateId ?? 0,
      JobId = jobId,
      ProjectStartDateId = jsd.ProjectStartDateId,
      Caption = jsd.Caption,
      Description = jsd.Description,
      Sequence = jsd.Sequence,
      DaysOffset = jsd.DaysOffset,
      StartDate = jsd.StartDate
      }).ToList();

      foreach (var result in results)
      {
          var startDateRevisions = (from sdr in dc.JobStartDateRevisions
                                      where sdr.Id == result.Id
                                      select new JobStartDateRevisionEntity
                                      { 
                                          Id = sdr.Id,
                                          JobId = sdr.JobId,
                                          StartDate = sdr.StartDate,
                                          Revision = sdr.Revision
                                      }).OrderByDescending(x => x.Revision).ToList();
      
          result.StartDateRevisions = startDateRevisions;
      }
      
      return results;
      

      }
      }

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      R Offline
      R Offline
      Richard Deeming
      wrote on last edited by
      #2

      Assuming you have a navigation property, you may be able to do something like this:

      var jobStartDates = dc.JobStartDates.AsNoTracking().Include(jsd => jsd.Revisions).Where(jsd => jsd.JobId == jobId).ToList();
      var results = jobStartDates.Select(jsd => new JobStartDateEntity
      {
      Id = jsd.Id,
      ...
      StartDateRevisions = jsd.Revisions.Select(sdr => new JobStartDateRevisionEntry
      {
      Id = sdr.Id,
      ...
      }).OrderByDescending(sdr => sdr.Revision).ToList(),
      });

      Alternatively, you can do it with two queries, rather than N+1 queries:

      var results = (from jsd in dc.JobStartDates
      where jsd.JobId == jobId
      select new JobStartDateEntity
      {
      ...
      }).ToList();

      var jobStartDateIds = results.Select(jsd => jsd.Id).ToList();

      var revisions = dc.JobStartDateRevisions
      .Where(sdr => jobStartDateIds.Contains(sdr.Id))
      .Select(sdr => new JobStartDateRevisionEntity { ... })
      .ToList();

      forech (var result in results)
      {
      result.StartDateRevisions = revisions
      .Where(sdr => sdr.Id == result.Id)
      .OrderByDescending(sdr => sdr.Revision)
      .ToList();
      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      N 1 Reply Last reply
      0
      • R Richard Deeming

        Assuming you have a navigation property, you may be able to do something like this:

        var jobStartDates = dc.JobStartDates.AsNoTracking().Include(jsd => jsd.Revisions).Where(jsd => jsd.JobId == jobId).ToList();
        var results = jobStartDates.Select(jsd => new JobStartDateEntity
        {
        Id = jsd.Id,
        ...
        StartDateRevisions = jsd.Revisions.Select(sdr => new JobStartDateRevisionEntry
        {
        Id = sdr.Id,
        ...
        }).OrderByDescending(sdr => sdr.Revision).ToList(),
        });

        Alternatively, you can do it with two queries, rather than N+1 queries:

        var results = (from jsd in dc.JobStartDates
        where jsd.JobId == jobId
        select new JobStartDateEntity
        {
        ...
        }).ToList();

        var jobStartDateIds = results.Select(jsd => jsd.Id).ToList();

        var revisions = dc.JobStartDateRevisions
        .Where(sdr => jobStartDateIds.Contains(sdr.Id))
        .Select(sdr => new JobStartDateRevisionEntity { ... })
        .ToList();

        forech (var result in results)
        {
        result.StartDateRevisions = revisions
        .Where(sdr => sdr.Id == result.Id)
        .OrderByDescending(sdr => sdr.Revision)
        .ToList();
        }


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        N Offline
        N Offline
        Nathan Minier
        wrote on last edited by
        #3

        Without a navigation property, wouldn't a join be easier and faster?

        "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

        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