title: Using LINQ to query/subquery multiple methods (Quartz.NET example for groups, jobs, and triggers)
body: <p>Definitely a LINQ newbie, but very experienced with SQL and C# and wondering if this is possible in LINQ. If so, I could use it other places, but I figured this would be a good starting point (and help to simplify/clean up some code). This could be more generalized, but I figured this might be a good real-life example that could help explain.</p>
  
  <p>Quick background: I'm doing a personal learning project building a scheduler and learning Spring.NET/DI, Fluent NHibernate, Quartz.NET, and trying to get my feat wet with TDD. Learned a ton so far.</p>
  
  <p>A Quartz.NET IScheduler object has these property(1)/methods(2) (assume public)...</p>
  
  <pre><code>string[] JobGroupNames { get; }
  string[] GetJobNames(string groupName)
  Trigger[] GetTriggersOfJob(string jobName, string groupName)
  </code></pre>
  
  <p>Assume Trigger definition is just...  </p>
  
  <pre><code>class Trigger
  {  
      string Name { get; }
  }  
  </code></pre>
  
  <p>I have a class I'm trying to get a list of which has a constructor like the following (because it's immutable once created)...</p>
  
  <pre><code>class QuartzJob
  {
      public QuartzJob(Guid groupId, Guid jobId, IEnumerable&lt;string&gt; triggerNames)
  }
  </code></pre>
  
  <p>Currently this is how I'm handling it...</p>
  
  <pre><code>public IEnumerable&lt;QuartzJob&gt; GetQuartzInfo(IScheduler scheduler)
  {
      List&lt;QuartzJob&gt; list = new List&lt;QuartzJob&gt;();
  
      foreach (string grp in scheduler.JobGroupNames)
      {
          foreach (string job in scheduler.GetJobNames(grp))
          {
              var triggerNames = scheduler
                  .GetTriggersOfJob(job, grp)
                  .ToList()
                  .ConvertAll(t =&gt; t.Name);
  
              var qj = new QuartzJob(new Guid(grp), new Guid(job), triggerNames);
              list.Add(qj);
          }
      }    
      return list;
  }
  </code></pre>
  
  <p>This way works fine (albeit maybe a little slow and complex), but those double foreach loops bug me and since I'm a "learn LINQ" kick, I thought this would a good chance and attempt to apply it.</p>
  
  <p>Not asking for someone to write the code for me, as this is a learning project (although you are more then welcome to), just trying to see if LINQ can do things like this, and if so, looking for some more information on it... Method calls with query values, and use those values to build another query. If so, it would reduce some of my multiple foreach loops I have in other places in my code.</p>
  
  <p>Thanks!</p>