2

I'm trying to figure out how to limit my child dataset to only include active records...

    // Here's what I have currently...
    m_BackLoggerEntities.Stories
     .Include("Sprints")
     .Include("Tasks")
     .Include("Efforts")
     .Include("Products")
     .First(s => s.StoryId == id);


    // Here's what I thought I could do...
    m_BackLoggerEntities.Stories
     .Include("Sprints")
     .Include("Tasks")
     .Include("Efforts")
     .Include("Products")
     .Where(s => s.Tasks.Active)
     .First(s => s.StoryId == id);


    // I also tried this...
    m_BackLoggerEntities.Stories
     .Include("Sprints")
     .Include("Tasks")
     .Include("Efforts")
     .Include("Products")
     .First(s => s.StoryId == id && s => s.Tasks.Active));

Obviously none of these are working. I'm not sure how else to do this...

1
  • 2
    What exactly is the problem? Are you getting an exception? Zero results? Too many results? ??? Commented Oct 29, 2009 at 16:22

3 Answers 3

2

You need something like this:

Model = m_BackLoggerEntities.Stories
    .Include("Sprints")
    .Include("Tasks")
    .Include("Efforts")
    .Include("Products")
    .SingleOrDefault(s => s.StoryId == id);

Then, in your view:

@foreach (var task in Model.Tasks.Where(t => t.Active))
Sign up to request clarification or add additional context in comments.

1 Comment

This is a very old question.... "Oct 29 '09" Thanks but I was trying to AVOID using the foreach. How it was implemented was inconsequential to me.
1

Take a look at Alex James Tip 37. According to example in linked article, it can be done like this:

var query = from story in m_BackLoggerEntities.Stories
            where story.StoryId == id
            select new {
                          story,
                          Tasks = from task in story.Tasks
                                  where task.Active
                                  select task
                       };

var stories = query
   .AsEnumerable()
   .Select(x => x.Story);

Each Story inside of "stories" should have only active Tasks.

Comments

1

The only way I've found to "simulate" what I want is to use...

        var storyToDetail =
            m_BackLoggerEntities.Stories
                .Include("Sprints")
                .Include("Tasks")
            .Include("Efforts")
            .Include("Products")
                .First(s => s.StoryId == id);

Then in the foreach in the view...

            <% foreach (var task in Model.Tasks.Where(t => t.Active))

But this of course brings back a lot more records then I want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.