none
filtering data in parent tables RRS feed

  • Question

  • I've been trying to get this working the way I want it to.  I have three tables.  I've got a LINQ query that returns an object with multiple nested objects.  The inner-most object/table has a filter on it.  At the outer levels, I want to only return those tables that have data inside them.

    My query so far: 

          var report = from p in dc.Projects
                 select new
                 {
                   p.Name,
                   Tasks = from t in p.Tasks
                       select new
                       {
                         t.Name,
                         TimeEntries = from te in t.TimeEntries
                                where te.StartDate.Value > DateTime.Parse("2010-10-06 00:00:00")
                                select new
                                {
                                  te.StartedOn,
                                  te.Note
                                }
                       }
                 };
    

    Notice I only return TimeEntries that have a specific date.  In its current state, the query returns Taks that don't have TimeEntries that fit my criteria, as well as Projects that have Tasks that don't have TimeEntries that fit my criteria.

    So, say I have a Task that doesn't have time the fits my criteria.  How do I prevent that Task from appearing in the result set?

    Thanks,

    Frank

     

    Monday, October 11, 2010 12:51 PM

Answers

  • Hello Frank,

    Please try the following query:

    var query = from p in dc.Projects
    	  from t in dc.Tasks
    	  from te in dc.TimeEntries
    	  where p.TaskID == t.TaskID && t.StartDate==te.StartDate && te.StartDate > DateTime.Parse("")
     	  select new 
    		{
    			ProjectName = p.Name,
    			TaskName= t.Name,
    			StateDate = te.StartedOn,
    			Note = te.Node
    		};
    

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have any feedback, please tell us.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by liurong luo Monday, October 18, 2010 1:58 AM
    Friday, October 15, 2010 4:48 AM