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。它是如何实施的对我来说无关紧要。