简体   繁体   English

ado.net实体框架在where子句中使用count

[英]ado.net entity framework using count in a where clause

I am trying to execute the following query but I get the wrong results back. 我正在尝试执行以下查询,但是返回错误的结果。

        foreach (var item in (from project in db.Projects
                              where project.Id == pProjectId
                              from task in project.Tasks
                              from taskItem in task.TaskItems
                              where taskItem.Velocities.Count() == 0 // not finished yet
                              select new
                              {
                                  ProjectId = pProjectId,
                                  PriorityId = task.Priorities.Id,
                                  TaskId = task.Id,
                                  ResourceId = taskItem.Resources.Id,
                                  EstimatedDuration = taskItem.EstimatedDuration,
                                  TaskItemId = taskItem.Id
                              }))
        {
        }

I am trying to generate objects from all the taskItems that don't have velocity related objects. 我正在尝试从所有没有与速度相关的对象的taskItem生成对象。 The table structure is that every taskItem may have many velocities. 表结构是每个taskItem可能具有许多速度。 Right before this call I give velocities to some of the items, yet they are not filtered by this where clause. 在此调用之前,我为某些项目提供了速度,但是它们未被该where子句过滤。 Am I doing something obvious wrong? 我在做明显的错误吗?

Edit: I think (after staring at the code for a while) that I need to specify some kind of grouping. 编辑:我认为(盯着代码一会儿之后),我需要指定某种分组。 I don't actually require any of the Velocity record details, but rather just a count of them that relate to the taskItems 实际上,我不需要任何Velocity记录详细信息,而只是需要与taskItems相关的计数

You could try moving the taskItem.Velocities.Count() inside the select new { } 您可以尝试将taskItem.Velocities.Count()移动到选择的新{}内部

Then do a select on the resultant list where the velicties count == 0 然后在结果列表上进行选择,其中velictes计数== 0

You need to have joins to reflect the relationships, currently you are doing a cartesian (cross) join (ie all combinations of project, task and taskItem). 您需要具有联接以反映关系,当前您正在执行笛卡尔(交叉)联接(即项目,task和taskItem的所有组合)。

Try something like: 尝试类似:

var res = from project in db.Projects
          where project.Id == pProjectId
          join task in project.Tasks on task.projectId equals project.projectId
          join ttaskItem in task.TaskItems on taskItem.taskId equals task.taskId
          where taskItem.Velocities.Count() == 0 // not finished yet
          select new {
            ProjectId = pProjectId,
            PriorityId = task.Priorities.Id,
            TaskId = task.Id,
            ResourceId = taskItem.Resources.Id,
            EstimatedDuration = taskItem.EstimatedDuration,
            TaskItemId = taskItem.Id
          };

Have you saved the changes to the entity model before the query? 查询之前是否将更改保存到实体模型?

The Entity model is querying your database to retrieve those values, if you are adding data to the entities BUT had not saved them to the db, the query can't know about those new values. 实体模型正在查询数据库以检索这些值,如果您要向实体添加数据但尚未将其保存到数据库中,则查询将无法得知这些新值。

Try db.SaveChanges() before the query. 在查询之前尝试db.SaveChanges()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM