Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架查询#1_C#_Entity Framework_Entity - Fatal编程技术网

C# 实体框架查询#1

C# 实体框架查询#1,c#,entity-framework,entity,C#,Entity Framework,Entity,我正在试图找出如何限制我的子数据集只包含活动记录 // Here's what I have currently... m_BackLoggerEntities.Stories .Include("Sprints") .Include("Tasks") .Include("Efforts") .Include("Products") .First(s => s.StoryId == id); // Here's

我正在试图找出如何限制我的子数据集只包含活动记录

    // 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));

显然,这些都不起作用。我不知道还能怎么做…

我发现“模拟”我想要的东西的唯一方法是使用

        var storyToDetail =
            m_BackLoggerEntities.Stories
                .Include("Sprints")
                .Include("Tasks")
            .Include("Efforts")
            .Include("Products")
                .First(s => s.StoryId == id);
然后在foreach的视图中

            <% foreach (var task in Model.Tasks.Where(t => t.Active))
t.Active)
但是这当然带来了比我想要的更多的记录。

看看。 根据链接文章中的示例,可以这样做:

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);
Model = m_BackLoggerEntities.Stories
    .Include("Sprints")
    .Include("Tasks")
    .Include("Efforts")
    .Include("Products")
    .SingleOrDefault(s => s.StoryId == id);

“故事”中的每个故事都应该只有活动任务。

您需要这样的内容:

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);
Model = m_BackLoggerEntities.Stories
    .Include("Sprints")
    .Include("Tasks")
    .Include("Efforts")
    .Include("Products")
    .SingleOrDefault(s => s.StoryId == id);
那么,在你看来:

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

到底是什么问题?你有例外吗?零结果?结果太多了???这是一个非常古老的问题。。。。“09年10月29日”谢谢,但我尽量避免使用foreach。它是如何实施的对我来说无关紧要。